forked from googollee/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter.go
57 lines (47 loc) · 1.25 KB
/
adapter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package socketio
// BroadcastAdaptor is the adaptor to handle broadcast.
type BroadcastAdaptor interface {
// Join lets socket join the t room.
Join(room string, socket Socket) error
// Leave let socket leave the room.
Leave(room string, socket Socket) error
// Send will send the message with args to room. If ignore is not nil, it won't send to the socket ignore.
Send(ignore Socket, room, message string, args []interface{}) error
}
var newBroadcast = newBroadcastDefault
type broadcast map[string]map[string]Socket
func newBroadcastDefault() BroadcastAdaptor {
return make(broadcast)
}
func (b broadcast) Join(room string, socket Socket) error {
sockets, ok := b[room]
if !ok {
sockets = make(map[string]Socket)
}
sockets[socket.Id()] = socket
b[room] = sockets
return nil
}
func (b broadcast) Leave(room string, socket Socket) error {
sockets, ok := b[room]
if !ok {
return nil
}
delete(sockets, socket.Id())
if len(sockets) == 0 {
delete(b, room)
return nil
}
b[room] = sockets
return nil
}
func (b broadcast) Send(ignore Socket, room, message string, args []interface{}) error {
sockets := b[room]
for id, s := range sockets {
if ignore != nil && ignore.Id() == id {
continue
}
s.Emit(message, args...)
}
return nil
}