@@ -134,13 +134,13 @@ export default class WSSignaling {
134134 let newOffer = new Offer ( message . sdp , Date . now ( ) , false ) ;
135135
136136 if ( this . isPrivate ) {
137- const pair = connectionPair . get ( connectionId ) ;
138- const otherSessionWs = pair [ 0 ] == ws ? pair [ 1 ] : pair [ 0 ] ;
139- if ( otherSessionWs ) {
140- newOffer . polite = true ;
141- otherSessionWs . send ( JSON . stringify ( { from : connectionId , to : "" , type : "offer" , data : newOffer } ) ) ;
142- } else {
143- ws . send ( JSON . stringify ( { type : "error" , message : ` ${ connectionId } : This connection id is not ready other session.` } ) ) ;
137+ if ( connectionPair . has ( connectionId ) ) {
138+ const pair = connectionPair . get ( connectionId ) ;
139+ const otherSessionWs = pair [ 0 ] == ws ? pair [ 1 ] : pair [ 0 ] ;
140+ if ( otherSessionWs ) {
141+ newOffer . polite = true ;
142+ otherSessionWs . send ( JSON . stringify ( { from : connectionId , to : "" , type : "offer" , data : newOffer } ) ) ;
143+ }
144144 }
145145 return ;
146146 }
@@ -160,39 +160,31 @@ export default class WSSignaling {
160160 connectionIds . add ( connectionId ) ;
161161 const newAnswer = new Answer ( message . sdp , Date . now ( ) ) ;
162162
163- let otherSessionWs = null ;
164-
165- if ( this . isPrivate ) {
166- const pair = connectionPair . get ( connectionId ) ;
167- otherSessionWs = pair [ 0 ] == ws ? pair [ 1 ] : pair [ 0 ] ;
168- } else {
169- const pair = connectionPair . get ( connectionId ) ;
170- otherSessionWs = pair [ 0 ] ;
171- connectionPair . set ( connectionId , [ otherSessionWs , ws ] ) ;
163+ if ( ! connectionPair . has ( connectionId ) ) {
164+ return ;
172165 }
173166
174- if ( this . isPrivate ) {
175- otherSessionWs . send ( JSON . stringify ( { from : connectionId , to : "" , type : "answer" , data : newAnswer } ) ) ;
176- return ;
167+ const pair = connectionPair . get ( connectionId ) ;
168+ const otherSessionWs = pair [ 0 ] == ws ? pair [ 1 ] : pair [ 0 ] ;
169+
170+ if ( ! this . isPrivate ) {
171+ connectionPair . set ( connectionId , [ otherSessionWs , ws ] ) ;
177172 }
178173
179- clients . forEach ( ( _v , k ) => {
180- if ( k == ws ) {
181- return ;
182- }
183- k . send ( JSON . stringify ( { from : connectionId , to : "" , type : "answer" , data : newAnswer } ) ) ;
184- } ) ;
174+ otherSessionWs . send ( JSON . stringify ( { from : connectionId , to : "" , type : "answer" , data : newAnswer } ) ) ;
185175 }
186176
187177 private onCandidate ( ws : WebSocket , message : any ) {
188178 const connectionId = message . connectionId ;
189179 const candidate = new Candidate ( message . candidate , message . sdpMLineIndex , message . sdpMid , Date . now ( ) ) ;
190180
191181 if ( this . isPrivate ) {
192- const pair = connectionPair . get ( connectionId ) ;
193- const otherSessionWs = pair [ 0 ] == ws ? pair [ 1 ] : pair [ 0 ] ;
194- if ( otherSessionWs ) {
195- otherSessionWs . send ( JSON . stringify ( { from : connectionId , to : "" , type : "candidate" , data : candidate } ) ) ;
182+ if ( connectionPair . has ( connectionId ) ) {
183+ const pair = connectionPair . get ( connectionId ) ;
184+ const otherSessionWs = pair [ 0 ] == ws ? pair [ 1 ] : pair [ 0 ] ;
185+ if ( otherSessionWs ) {
186+ otherSessionWs . send ( JSON . stringify ( { from : connectionId , to : "" , type : "candidate" , data : candidate } ) ) ;
187+ }
196188 }
197189 return ;
198190 }
0 commit comments