Skip to content

Commit 8bf6c84

Browse files
committed
update.
1 parent 3f466f4 commit 8bf6c84

File tree

3 files changed

+67
-88
lines changed

3 files changed

+67
-88
lines changed

.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Dependency directories (remove the comment below to include it)
15+
# vendor/
16+
17+
.DS_Store

pkg/.DS_Store

-6 KB
Binary file not shown.

pkg/signaler/signaler.go

+50-88
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ type TurnCredentials struct {
2929
Uris []string `json:"uris"`
3030
}
3131

32-
// PeerInfo .
33-
type PeerInfo struct {
34-
ID string `json:"id"`
35-
Name string `json:"name"`
36-
UserAgent string `json:"user_agent"`
37-
}
38-
3932
// Peer .
4033
type Peer struct {
4134
info PeerInfo
@@ -66,9 +59,9 @@ type Request struct {
6659
Data interface{} `json:"data"`
6760
}
6861

69-
type Login struct {
70-
Name string `json:"name"`
62+
type PeerInfo struct {
7163
ID string `json:"id"`
64+
Name string `json:"name"`
7265
UserAgent string `json:"user_agent"`
7366
}
7467

@@ -219,28 +212,19 @@ func (s *Signaler) HandleNewWebSocket(conn *websocket.WebSocketConn, request *ht
219212

220213
switch request.Type {
221214
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,
240224
}
225+
s.NotifyPeersUpdate(conn, s.peers)
241226
break
242227
case Leave:
243-
break
244228
case Offer:
245229
fallthrough
246230
case Answer:
@@ -270,81 +254,59 @@ func (s *Signaler) HandleNewWebSocket(conn *websocket.WebSocketConn, request *ht
270254
}
271255
break
272256
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+
}
280263

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+
},
312272
}
273+
s.Send(conn, msg)
274+
return
275+
}
313276

314-
if peer, ok := s.peers[ids[1]]; !ok {
277+
sendBye := func(id string) {
278+
peer, ok := s.peers[id]
279+
280+
if !ok {
315281
msg := Request{
316282
Type: "error",
317283
Data: Error{
318284
Request: string(request.Type),
319-
Reason: "Peer [" + ids[0] + "] not found ",
285+
Reason: "Peer [" + id + "] not found.",
320286
},
321287
}
322288
s.Send(conn, msg)
323289
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)
333290
}
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)
334299
}
335-
break
300+
301+
// send to aleg
302+
sendBye(ids[0])
303+
//send to bleg
304+
sendBye(ids[1])
305+
336306
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)
343308
default:
344-
{
345-
logger.Warnf("Unkown request %v", request)
346-
}
347-
break
309+
logger.Warnf("Unkown request %v", request)
348310
}
349311
})
350312

0 commit comments

Comments
 (0)