-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
conn_namespace.go
313 lines (253 loc) · 6.9 KB
/
conn_namespace.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package neffos
import (
"context"
"reflect"
"sync"
)
// NSConn describes a connection connected to a specific namespace,
// it emits with the `Message.Namespace` filled and it can join to multiple rooms.
// A single `Conn` can be connected to one or more namespaces,
// each connected namespace is described by this structure.
type NSConn struct {
Conn *Conn
// Static from server, client can select which to use or not.
// Client and server can ask to connect.
// Server can forcely disconnect.
namespace string
// Static from server, client can select which to use or not.
events Events
// Dynamically channels/rooms for each connected namespace.
// Client can ask to join, server can forcely join a connection to a room.
// Namespace(room(fire event)).
rooms map[string]*Room
roomsMutex sync.RWMutex
// value is just a temporarily value.
// Storage across event callbacks for this namespace.
value reflect.Value
}
func newNSConn(c *Conn, namespace string, events Events) *NSConn {
return &NSConn{
Conn: c,
namespace: namespace,
events: events,
rooms: make(map[string]*Room),
}
}
// String method simply returns the Conn's ID().
// Useful method to this connected to a namespace connection to be passed on `Server#Broadcast` method
// to exclude itself from the broadcasted message's receivers.
func (ns *NSConn) String() string {
return ns.Conn.String()
}
// Emit method sends a message to the remote side
// with its `Message.Namespace` filled to this specific namespace.
func (ns *NSConn) Emit(event string, body []byte) bool {
if ns == nil {
return false
}
return ns.Conn.Write(Message{Namespace: ns.namespace, Event: event, Body: body})
}
// EmitBinary acts like `Emit` but it sets the `Message.SetBinary` to true
// and sends the data as binary, the receiver's Message in javascript-side is Uint8Array.
func (ns *NSConn) EmitBinary(event string, body []byte) bool {
if ns == nil {
return false
}
return ns.Conn.Write(Message{Namespace: ns.namespace, Event: event, Body: body, SetBinary: true})
}
// Ask method writes a message to the remote side and blocks until a response or an error received.
func (ns *NSConn) Ask(ctx context.Context, event string, body []byte) (Message, error) {
if ns == nil {
return Message{}, ErrWrite
}
return ns.Conn.Ask(ctx, Message{Namespace: ns.namespace, Event: event, Body: body})
}
// JoinRoom method can be used to join a connection to a specific room, rooms are dynamic.
// Returns the joined `Room`.
func (ns *NSConn) JoinRoom(ctx context.Context, roomName string) (*Room, error) {
if ns == nil {
return nil, ErrWrite
}
return ns.askRoomJoin(ctx, roomName)
}
// Room method returns a joined `Room`.
func (ns *NSConn) Room(roomName string) *Room {
if ns == nil {
return nil
}
ns.roomsMutex.RLock()
room := ns.rooms[roomName]
ns.roomsMutex.RUnlock()
return room
}
// Rooms returns a slice copy of the joined rooms.
func (ns *NSConn) Rooms() []*Room {
ns.roomsMutex.RLock()
rooms := make([]*Room, len(ns.rooms))
i := 0
for _, room := range ns.rooms {
rooms[i] = room
i++
}
ns.roomsMutex.RUnlock()
return rooms
}
// LeaveAll method sends a remote and local leave room signal `OnRoomLeave` to and for all rooms
// and fires the `OnRoomLeft` event if succeed.
func (ns *NSConn) LeaveAll(ctx context.Context) error {
if ns == nil {
return nil
}
ns.roomsMutex.Lock()
defer ns.roomsMutex.Unlock()
leaveMsg := Message{Namespace: ns.namespace, Event: OnRoomLeave, IsLocal: true, locked: true}
for room := range ns.rooms {
leaveMsg.Room = room
if err := ns.askRoomLeave(ctx, leaveMsg, false); err != nil {
return err
}
}
return nil
}
func (ns *NSConn) forceLeaveAll(isLocal bool) {
ns.roomsMutex.Lock()
defer ns.roomsMutex.Unlock()
leaveMsg := Message{Namespace: ns.namespace, Event: OnRoomLeave, IsForced: true, IsLocal: isLocal}
for room := range ns.rooms {
leaveMsg.Room = room
ns.events.fireEvent(ns, leaveMsg)
delete(ns.rooms, room)
leaveMsg.Event = OnRoomLeft
ns.events.fireEvent(ns, leaveMsg)
leaveMsg.Event = OnRoomLeave
}
}
// Disconnect method sends a disconnect signal to the remote side and fires the local `OnNamespaceDisconnect` event.
func (ns *NSConn) Disconnect(ctx context.Context) error {
if ns == nil {
return nil
}
return ns.Conn.askDisconnect(ctx, Message{
Namespace: ns.namespace,
Event: OnNamespaceDisconnect,
}, true)
}
func (ns *NSConn) askRoomJoin(ctx context.Context, roomName string) (*Room, error) {
ns.roomsMutex.RLock()
room, ok := ns.rooms[roomName]
ns.roomsMutex.RUnlock()
if ok {
return room, nil
}
joinMsg := Message{
Namespace: ns.namespace,
Room: roomName,
Event: OnRoomJoin,
IsLocal: true,
}
_, err := ns.Conn.Ask(ctx, joinMsg)
if err != nil {
return nil, err
}
err = ns.events.fireEvent(ns, joinMsg)
if err != nil {
return nil, err
}
room = newRoom(ns, roomName)
ns.roomsMutex.Lock()
ns.rooms[roomName] = room
ns.roomsMutex.Unlock()
joinMsg.Event = OnRoomJoined
ns.events.fireEvent(ns, joinMsg)
return room, nil
}
func (ns *NSConn) replyRoomJoin(msg Message) {
if ns == nil || msg.wait == "" || msg.isNoOp {
return
}
ns.roomsMutex.RLock()
_, ok := ns.rooms[msg.Room]
ns.roomsMutex.RUnlock()
if !ok {
err := ns.events.fireEvent(ns, msg)
if err != nil {
msg.Err = err
ns.Conn.Write(msg)
return
}
ns.roomsMutex.Lock()
ns.rooms[msg.Room] = newRoom(ns, msg.Room)
ns.roomsMutex.Unlock()
msg.Event = OnRoomJoined
ns.events.fireEvent(ns, msg)
}
ns.Conn.writeEmptyReply(msg.wait)
}
func (ns *NSConn) askRoomLeave(ctx context.Context, msg Message, lock bool) error {
if ns == nil {
return nil
}
if lock {
ns.roomsMutex.RLock()
}
_, ok := ns.rooms[msg.Room]
if lock {
ns.roomsMutex.RUnlock()
}
if !ok {
return ErrBadRoom
}
_, err := ns.Conn.Ask(ctx, msg)
if err != nil {
return err
}
// msg.IsLocal = true
err = ns.events.fireEvent(ns, msg)
if err != nil {
return err
}
if lock {
ns.roomsMutex.Lock()
}
delete(ns.rooms, msg.Room)
if lock {
ns.roomsMutex.Unlock()
}
msg.Event = OnRoomLeft
ns.events.fireEvent(ns, msg)
return nil
}
func (ns *NSConn) replyRoomLeave(msg Message) {
if ns == nil || msg.wait == "" || msg.isNoOp {
return
}
room := ns.Room(msg.Room)
if room == nil {
ns.Conn.writeEmptyReply(msg.wait)
return
}
// if client then we need to respond to server and delete the room without ask the local event.
if ns.Conn.IsClient() {
ns.events.fireEvent(ns, msg)
ns.roomsMutex.Lock()
delete(ns.rooms, msg.Room)
ns.roomsMutex.Unlock()
ns.Conn.writeEmptyReply(msg.wait)
msg.Event = OnRoomLeft
ns.events.fireEvent(ns, msg)
return
}
// server-side, check for error on the local event first.
err := ns.events.fireEvent(ns, msg)
if err != nil {
msg.Err = err
ns.Conn.Write(msg)
return
}
ns.roomsMutex.Lock()
delete(ns.rooms, msg.Room)
ns.roomsMutex.Unlock()
msg.Event = OnRoomLeft
ns.events.fireEvent(ns, msg)
ns.Conn.writeEmptyReply(msg.wait)
}