forked from googollee/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
337 lines (273 loc) · 7.03 KB
/
server.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package socketio
import (
"net/http"
"github.com/gomodule/redigo/redis"
"github.com/googollee/go-socket.io/engineio"
"github.com/googollee/go-socket.io/parser"
)
// Server is a go-socket.io server.
type Server struct {
engine *engineio.Server
handlers *namespaceHandlers
redisAdapter *RedisAdapterOptions
}
// NewServer returns a server.
func NewServer(opts *engineio.Options) *Server {
return &Server{
handlers: newNamespaceHandlers(),
engine: engineio.NewServer(opts),
}
}
// Adapter sets redis broadcast adapter.
func (s *Server) Adapter(opts *RedisAdapterOptions) (bool, error) {
opts = getOptions(opts)
var redisOpts []redis.DialOption
if len(opts.Password) > 0 {
redisOpts = append(redisOpts, redis.DialPassword(opts.Password))
}
conn, err := redis.Dial(opts.Network, opts.getAddr(), redisOpts...)
if err != nil {
return false, err
}
s.redisAdapter = opts
conn.Close()
return true, nil
}
// Close closes server.
func (s *Server) Close() error {
return s.engine.Close()
}
// ServeHTTP dispatches the request to the handler whose pattern most closely matches the request URL.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.engine.ServeHTTP(w, r)
}
// OnConnect set a handler function f to handle open event for namespace.
func (s *Server) OnConnect(namespace string, f func(Conn) error) {
h := s.getNamespace(namespace)
if h == nil {
h = s.createNamespace(namespace)
}
h.OnConnect(f)
}
// OnDisconnect set a handler function f to handle disconnect event for namespace.
func (s *Server) OnDisconnect(namespace string, f func(Conn, string)) {
h := s.getNamespace(namespace)
if h == nil {
h = s.createNamespace(namespace)
}
h.OnDisconnect(f)
}
// OnError set a handler function f to handle error for namespace.
func (s *Server) OnError(namespace string, f func(Conn, error)) {
h := s.getNamespace(namespace)
if h == nil {
h = s.createNamespace(namespace)
}
h.OnError(f)
}
// OnEvent set a handler function f to handle event for namespace.
func (s *Server) OnEvent(namespace, event string, f interface{}) {
h := s.getNamespace(namespace)
if h == nil {
h = s.createNamespace(namespace)
}
h.OnEvent(event, f)
}
// Serve serves go-socket.io server.
func (s *Server) Serve() error {
for {
conn, err := s.engine.Accept()
//todo maybe need check EOF from Accept()
if err != nil {
return err
}
go s.serveConn(conn)
}
}
// JoinRoom joins given connection to the room.
func (s *Server) JoinRoom(namespace string, room string, connection Conn) bool {
nspHandler := s.getNamespace(namespace)
if nspHandler != nil {
nspHandler.broadcast.Join(room, connection)
return true
}
return false
}
// LeaveRoom leaves given connection from the room.
func (s *Server) LeaveRoom(namespace string, room string, connection Conn) bool {
nspHandler := s.getNamespace(namespace)
if nspHandler != nil {
nspHandler.broadcast.Leave(room, connection)
return true
}
return false
}
// LeaveAllRooms leaves the given connection from all rooms.
func (s *Server) LeaveAllRooms(namespace string, connection Conn) bool {
nspHandler := s.getNamespace(namespace)
if nspHandler != nil {
nspHandler.broadcast.LeaveAll(connection)
return true
}
return false
}
// ClearRoom clears the room.
func (s *Server) ClearRoom(namespace string, room string) bool {
nspHandler := s.getNamespace(namespace)
if nspHandler != nil {
nspHandler.broadcast.Clear(room)
return true
}
return false
}
// BroadcastToRoom broadcasts given event & args to all the connections in the room.
func (s *Server) BroadcastToRoom(namespace string, room, event string, args ...interface{}) bool {
nspHandler := s.getNamespace(namespace)
if nspHandler != nil {
nspHandler.broadcast.Send(room, event, args...)
return true
}
return false
}
// BroadcastToNamespace broadcasts given event & args to all the connections in the same namespace.
func (s *Server) BroadcastToNamespace(namespace string, event string, args ...interface{}) bool {
nspHandler := s.getNamespace(namespace)
if nspHandler != nil {
nspHandler.broadcast.SendAll(event, args...)
return true
}
return false
}
// RoomLen gives number of connections in the room.
func (s *Server) RoomLen(namespace string, room string) int {
nspHandler := s.getNamespace(namespace)
if nspHandler != nil {
return nspHandler.broadcast.Len(room)
}
return -1
}
// Rooms gives list of all the rooms.
func (s *Server) Rooms(namespace string) []string {
nspHandler := s.getNamespace(namespace)
if nspHandler != nil {
return nspHandler.broadcast.Rooms(nil)
}
return nil
}
// Count number of connections.
func (s *Server) Count() int {
return s.engine.Count()
}
// ForEach sends data by DataFunc, if room does not exit sends anything.
func (s *Server) ForEach(namespace string, room string, f EachFunc) bool {
nspHandler := s.getNamespace(namespace)
if nspHandler != nil {
nspHandler.broadcast.ForEach(room, f)
return true
}
return false
}
func (s *Server) serveConn(conn engineio.Conn) {
c := newConn(conn, s.handlers)
if err := c.connect(); err != nil {
_ = c.Close()
if root, ok := s.handlers.Get(rootNamespace); ok && root.onError != nil {
root.onError(nil, err)
}
return
}
go s.serveError(c)
go s.serveWrite(c)
go s.serveRead(c)
}
func (s *Server) serveError(c *conn) {
defer func() {
c.Close()
s.engine.Remove(c.ID())
}()
for {
select {
case <-c.quitChan:
return
case err := <-c.errorChan:
errMsg, ok := err.(errorMessage)
if !ok {
continue
}
if handler := c.namespace(errMsg.namespace); handler != nil {
if handler.onError != nil {
nsConn, ok := c.namespaces.Get(errMsg.namespace)
if !ok {
continue
}
handler.onError(nsConn, errMsg.err)
}
}
}
}
}
func (s *Server) serveWrite(c *conn) {
defer func() {
c.Close()
s.engine.Remove(c.ID())
}()
for {
select {
case <-c.quitChan:
return
case pkg := <-c.writeChan:
if err := c.encoder.Encode(pkg.Header, pkg.Data); err != nil {
c.onError(pkg.Header.Namespace, err)
}
}
}
}
func (s *Server) serveRead(c *conn) {
defer func() {
c.Close()
s.engine.Remove(c.ID())
}()
var event string
for {
var header parser.Header
if err := c.decoder.DecodeHeader(&header, &event); err != nil {
c.onError(rootNamespace, err)
return
}
if header.Namespace == aliasRootNamespace {
header.Namespace = rootNamespace
}
var err error
switch header.Type {
case parser.Ack, parser.Connect, parser.Disconnect:
handler, ok := readHandlerMapping[header.Type]
if !ok {
return
}
err = handler(c, header)
case parser.Event:
err = eventPacketHandler(c, event, header)
}
if err != nil {
return
}
}
}
func (s *Server) createNamespace(nsp string) *namespaceHandler {
if nsp == aliasRootNamespace {
nsp = rootNamespace
}
handler := newNamespaceHandler(nsp, s.redisAdapter)
s.handlers.Set(nsp, handler)
return handler
}
func (s *Server) getNamespace(nsp string) *namespaceHandler {
if nsp == aliasRootNamespace {
nsp = rootNamespace
}
ret, ok := s.handlers.Get(nsp)
if !ok {
return nil
}
return ret
}