forked from googollee/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.go
70 lines (59 loc) · 1.45 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
58
59
60
61
62
63
64
65
66
67
68
69
70
package socketio
import "sync"
// BroadcastAdaptor is the adaptor to handle broadcasts.
type BroadcastAdaptor interface {
// Join causes the socket to join a room.
Join(room string, socket Socket) error
// Leave causes the socket to leave a room.
Leave(room string, socket Socket) error
// Send will send an event with args to the room. If "ignore" is not nil, the event will be excluded from being sent to "ignore".
Send(ignore Socket, room, event string, args ...interface{}) error
}
var newBroadcast = newBroadcastDefault
type broadcast struct {
m map[string]map[string]Socket
sync.RWMutex
}
func newBroadcastDefault() BroadcastAdaptor {
return &broadcast{
m: make(map[string]map[string]Socket),
}
}
func (b *broadcast) Join(room string, socket Socket) error {
b.Lock()
sockets, ok := b.m[room]
if !ok {
sockets = make(map[string]Socket)
}
sockets[socket.Id()] = socket
b.m[room] = sockets
b.Unlock()
return nil
}
func (b *broadcast) Leave(room string, socket Socket) error {
b.Lock()
defer b.Unlock()
sockets, ok := b.m[room]
if !ok {
return nil
}
delete(sockets, socket.Id())
if len(sockets) == 0 {
delete(b.m, room)
return nil
}
b.m[room] = sockets
return nil
}
func (b *broadcast) Send(ignore Socket, room, event string, args ...interface{}) error {
b.RLock()
sockets := b.m[room]
for id, s := range sockets {
if ignore != nil && ignore.Id() == id {
continue
}
s.Emit(event, args...)
}
b.RUnlock()
return nil
}