@@ -29,13 +29,6 @@ type TurnCredentials struct {
29
29
Uris []string `json:"uris"`
30
30
}
31
31
32
- // PeerInfo .
33
- type PeerInfo struct {
34
- ID string `json:"id"`
35
- Name string `json:"name"`
36
- UserAgent string `json:"user_agent"`
37
- }
38
-
39
32
// Peer .
40
33
type Peer struct {
41
34
info PeerInfo
@@ -66,9 +59,9 @@ type Request struct {
66
59
Data interface {} `json:"data"`
67
60
}
68
61
69
- type Login struct {
70
- Name string `json:"name"`
62
+ type PeerInfo struct {
71
63
ID string `json:"id"`
64
+ Name string `json:"name"`
72
65
UserAgent string `json:"user_agent"`
73
66
}
74
67
@@ -219,28 +212,19 @@ func (s *Signaler) HandleNewWebSocket(conn *websocket.WebSocketConn, request *ht
219
212
220
213
switch request .Type {
221
214
case New :
222
- {
223
- var login Login
224
- err := json .Unmarshal (body , & login )
225
- if err != nil {
226
- logger .Errorf ("Unmarshal login error %v" , err )
227
- return
228
- }
229
-
230
- peer := Peer {
231
- conn : conn ,
232
- info : PeerInfo {
233
- ID : login .ID ,
234
- Name : login .Name ,
235
- UserAgent : login .UserAgent ,
236
- },
237
- }
238
- s .peers [peer .info .ID ] = peer
239
- s .NotifyPeersUpdate (conn , s .peers )
215
+ var info PeerInfo
216
+ err := json .Unmarshal (body , & info )
217
+ if err != nil {
218
+ logger .Errorf ("Unmarshal login error %v" , err )
219
+ return
220
+ }
221
+ s .peers [info .ID ] = Peer {
222
+ conn : conn ,
223
+ info : info ,
240
224
}
225
+ s .NotifyPeersUpdate (conn , s .peers )
241
226
break
242
227
case Leave :
243
- break
244
228
case Offer :
245
229
fallthrough
246
230
case Answer :
@@ -270,81 +254,59 @@ func (s *Signaler) HandleNewWebSocket(conn *websocket.WebSocketConn, request *ht
270
254
}
271
255
break
272
256
case Bye :
273
- {
274
- var bye Byebye
275
- err := json .Unmarshal (body , & bye )
276
- if err != nil {
277
- logger .Errorf ("Unmarshal bye got error %v" , err )
278
- return
279
- }
257
+ var bye Byebye
258
+ err := json .Unmarshal (body , & bye )
259
+ if err != nil {
260
+ logger .Errorf ("Unmarshal bye got error %v" , err )
261
+ return
262
+ }
280
263
281
- ids := strings .Split (bye .SessionID , "-" )
282
- if len (ids ) != 2 {
283
- msg := Request {
284
- Type : "error" ,
285
- Data : Error {
286
- Request : string (request .Type ),
287
- Reason : "Invalid session [" + bye .SessionID + "]" ,
288
- },
289
- }
290
- s .Send (conn , msg )
291
- return
292
- }
293
- if peer , ok := s .peers [ids [0 ]]; ! ok {
294
- msg := Request {
295
- Type : "error" ,
296
- Data : Error {
297
- Request : string (request .Type ),
298
- Reason : "Peer [" + ids [0 ] + "] not found." ,
299
- },
300
- }
301
- s .Send (conn , msg )
302
- return
303
- } else {
304
- bye := Request {
305
- Type : "bye" ,
306
- Data : map [string ]interface {}{
307
- "to" : ids [0 ],
308
- "session_id" : bye .SessionID ,
309
- },
310
- }
311
- s .Send (peer .conn , bye )
264
+ ids := strings .Split (bye .SessionID , "-" )
265
+ if len (ids ) != 2 {
266
+ msg := Request {
267
+ Type : "error" ,
268
+ Data : Error {
269
+ Request : string (request .Type ),
270
+ Reason : "Invalid session [" + bye .SessionID + "]" ,
271
+ },
312
272
}
273
+ s .Send (conn , msg )
274
+ return
275
+ }
313
276
314
- if peer , ok := s .peers [ids [1 ]]; ! ok {
277
+ sendBye := func (id string ) {
278
+ peer , ok := s .peers [id ]
279
+
280
+ if ! ok {
315
281
msg := Request {
316
282
Type : "error" ,
317
283
Data : Error {
318
284
Request : string (request .Type ),
319
- Reason : "Peer [" + ids [ 0 ] + "] not found " ,
285
+ Reason : "Peer [" + id + "] not found. " ,
320
286
},
321
287
}
322
288
s .Send (conn , msg )
323
289
return
324
- } else {
325
- bye := Request {
326
- Type : "bye" ,
327
- Data : map [string ]interface {}{
328
- "to" : ids [1 ],
329
- "session_id" : bye .SessionID ,
330
- },
331
- }
332
- s .Send (peer .conn , bye )
333
290
}
291
+ bye := Request {
292
+ Type : "bye" ,
293
+ Data : map [string ]interface {}{
294
+ "to" : id ,
295
+ "session_id" : bye .SessionID ,
296
+ },
297
+ }
298
+ s .Send (peer .conn , bye )
334
299
}
335
- break
300
+
301
+ // send to aleg
302
+ sendBye (ids [0 ])
303
+ //send to bleg
304
+ sendBye (ids [1 ])
305
+
336
306
case Keepalive :
337
- keepalive := Request {
338
- Type : request .Type ,
339
- Data : make (map [string ]interface {}),
340
- }
341
- s .Send (conn , keepalive )
342
- break
307
+ s .Send (conn , request )
343
308
default :
344
- {
345
- logger .Warnf ("Unkown request %v" , request )
346
- }
347
- break
309
+ logger .Warnf ("Unkown request %v" , request )
348
310
}
349
311
})
350
312
0 commit comments