-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathhub.go
209 lines (182 loc) · 5.31 KB
/
hub.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package ss
type socketHub struct {
sockets map[string]*Socket
rooms map[string]*room
shutdownCh chan bool
socketList chan []*Socket
addCh chan *Socket
delCh chan *Socket
joinRoomCh chan *joinRequest
leaveRoomCh chan *leaveRequest
roomMsgCh chan *RoomMsg
broomcastCh chan *RoomMsg //for passing data from the backend
broadcastCh chan *BroadcastMsg
bbroadcastCh chan *BroadcastMsg
multihomeEnabled bool
multihomeBackend MultihomeBackend
}
type room struct {
name string
sockets map[string]*Socket
}
type joinRequest struct {
roomName string
socket *Socket
}
type leaveRequest struct {
roomName string
socket *Socket
}
//RoomMsg represents an event to be dispatched to a room of sockets
type RoomMsg struct {
RoomName string
EventName string
Data interface{}
}
//BroadcastMsg represents an event to be dispatched to all Sockets on the SocketServer
type BroadcastMsg struct {
EventName string
Data interface{}
}
func (h *socketHub) addSocket(s *Socket) {
h.addCh <- s
}
func (h *socketHub) removeSocket(s *Socket) {
h.delCh <- s
}
func (h *socketHub) joinRoom(j *joinRequest) {
h.joinRoomCh <- j
}
func (h *socketHub) leaveRoom(l *leaveRequest) {
h.leaveRoomCh <- l
}
func (h *socketHub) roomcast(msg *RoomMsg) {
h.roomMsgCh <- msg
}
func (h *socketHub) broadcast(b *BroadcastMsg) {
h.broadcastCh <- b
}
func (h *socketHub) setMultihomeBackend(b MultihomeBackend) {
if h.multihomeEnabled {
return //can't have two backends... yet
}
h.multihomeBackend = b
h.multihomeEnabled = true
h.multihomeBackend.Init()
go h.multihomeBackend.BroadcastFromBackend(h.bbroadcastCh)
go h.multihomeBackend.RoomcastFromBackend(h.broomcastCh)
}
func (h *socketHub) listen() {
for {
select {
case c := <-h.addCh:
h.sockets[c.ID()] = c
case c := <-h.delCh:
delete(h.sockets, c.ID())
case c := <-h.joinRoomCh:
if _, exists := h.rooms[c.roomName]; !exists { //make the room if it doesn't exist
h.rooms[c.roomName] = &room{c.roomName, make(map[string]*Socket)}
}
h.rooms[c.roomName].sockets[c.socket.ID()] = c.socket
case c := <-h.leaveRoomCh:
if room, exists := h.rooms[c.roomName]; exists {
delete(room.sockets, c.socket.ID())
if len(room.sockets) == 0 { //room is empty, delete it
delete(h.rooms, c.roomName)
}
}
case c := <-h.roomMsgCh:
if room, exists := h.rooms[c.RoomName]; exists {
for _, s := range room.sockets {
s.Emit(c.EventName, c.Data)
}
}
if h.multihomeEnabled { //the room may exist on the other end
go h.multihomeBackend.RoomcastToBackend(c)
}
case c := <-h.broomcastCh:
if room, exists := h.rooms[c.RoomName]; exists {
for _, s := range room.sockets {
s.Emit(c.EventName, c.Data)
}
}
case c := <-h.broadcastCh:
for _, s := range h.sockets {
s.Emit(c.EventName, c.Data)
}
if h.multihomeEnabled {
go h.multihomeBackend.BroadcastToBackend(c)
}
case c := <-h.bbroadcastCh:
for _, s := range h.sockets {
s.Emit(c.EventName, c.Data)
}
case _ = <-h.shutdownCh:
var socketList []*Socket
for _, s := range h.sockets {
socketList = append(socketList, s)
}
h.socketList <- socketList
}
}
}
func newHub() *socketHub {
h := &socketHub{
shutdownCh: make(chan bool),
socketList: make(chan []*Socket),
sockets: make(map[string]*Socket),
rooms: make(map[string]*room),
addCh: make(chan *Socket),
delCh: make(chan *Socket),
joinRoomCh: make(chan *joinRequest),
leaveRoomCh: make(chan *leaveRequest),
roomMsgCh: make(chan *RoomMsg),
broomcastCh: make(chan *RoomMsg),
broadcastCh: make(chan *BroadcastMsg),
bbroadcastCh: make(chan *BroadcastMsg),
multihomeEnabled: false,
}
go h.listen()
return h
}
//MultihomeBackend is an interface for implementing a mechanism
//to syncronize Broadcasts and Roomcasts to multiple SocketServers
//running separate machines.
//
//Sacrificial-Socket provides a MultihomeBackend for use with MongoDB
//in sacrificial-socket/backend
type MultihomeBackend interface {
//Init is called as soon as the MultihomeBackend is
//registered using SocketServer.SetMultihomeBackend
Init()
//Shutdown is called immediately after all sockets have
//been closed
Shutdown()
//BroadcastToBackend is called everytime a BroadcastMsg is
//sent by a Socket
//
//BroadcastToBackend must be safe for concurrent use by multiple
//go routines
BroadcastToBackend(*BroadcastMsg)
//RoomcastToBackend is called everytime a RoomMsg is sent
//by a socket, even if none of this server's sockets are
//members of that room
//
//RoomcastToBackend must be safe for concurrent use by multiple
//go routines
RoomcastToBackend(*RoomMsg)
//BroadcastFromBackend is called once and only once as a go routine as
//soon as the MultihomeBackend is registered using
//SocketServer.SetMultihomeBackend
//
//b consumes a BroadcastMsg and dispatches
//it to all sockets on this server
BroadcastFromBackend(b chan<- *BroadcastMsg)
//RoomcastFromBackend is called once and only once as a go routine as
//soon as the MultihomeBackend is registered using
//SocketServer.SetMultihomeBackend
//
//r consumes a RoomMsg and dispatches it to all sockets
//that are members the specified room
RoomcastFromBackend(r chan<- *RoomMsg)
}