-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.go
194 lines (179 loc) · 4.91 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
package socketio
import (
"net/http"
"sync"
"time"
"github.com/zyxar/socketio/engine"
)
// Server is socket.io server implementation
type Server struct {
engine *engine.Server
sockets map[*engine.Socket]*socket
sockLock sync.RWMutex
onError func(err error)
nsps map[string]*namespace
}
// NewServer creates a socket.io server instance upon underlying engine.io transport
func NewServer(interval, timeout time.Duration, parser Parser, oc ...engine.OriginChecker) (server *Server, err error) {
e, err := engine.NewServer(interval, timeout, func(ß *engine.Socket) {
socket := newSocket(ß, parser)
socket.attachnsp("/")
nsp := server.creatensp("/")
if err := socket.emitPacket(&Packet{
Type: PacketTypeConnect,
Namespace: "/",
}); err != nil {
if nsp.onError != nil {
nsp.onError(socket, err)
}
}
server.sockLock.Lock()
server.sockets[ß] = socket
server.sockLock.Unlock()
if nsp.onConnect != nil {
nsp.onConnect(socket)
}
}, oc...)
if err != nil {
return
}
server = &Server{engine: e, sockets: make(map[*engine.Socket]*socket), nsps: make(map[string]*namespace)}
e.On(engine.EventMessage, engine.Callback(func(ß *engine.Socket, msgType engine.MessageType, data []byte) {
server.sockLock.RLock()
socket := server.sockets[ß]
server.sockLock.RUnlock()
if socket == nil {
return
}
switch msgType {
case engine.MessageTypeString:
case engine.MessageTypeBinary:
default:
return
}
if err := socket.decoder.Add(msgType, data); err != nil {
if server.onError != nil {
server.onError(err)
}
}
if p := socket.yield(); p != nil {
server.process(socket, p)
}
}))
e.On(engine.EventClose, engine.Callback(func(ß *engine.Socket, _ engine.MessageType, _ []byte) {
server.sockLock.RLock()
socket := server.sockets[ß]
server.sockLock.RUnlock()
if socket == nil {
return
}
server.sockLock.Lock()
delete(server.sockets, ß)
server.sockLock.Unlock()
socket.Close()
detachall(server, socket)
}))
return
}
// Namespace ensures a Namespace instance exists in server
func (s *Server) Namespace(nsp string) Namespace { return s.creatensp(nsp) }
func (s *Server) creatensp(nsp string) *namespace {
n, ok := s.nsps[nsp]
if !ok {
n = &namespace{callbacks: make(map[string]*callback)}
s.nsps[nsp] = n
}
return n
}
func (s *Server) getnsp(nsp string) (n *namespace, ok bool) { n, ok = s.nsps[nsp]; return }
// ServeHTTP implements http.Handler interface
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.engine.ServeHTTP(w, r) }
// Close closes underlying engine.io transport
func (s *Server) Close() error { return s.engine.Close() }
// OnError registers fn as callback for error handling
func (s *Server) OnError(fn func(err error)) { s.onError = fn }
// process is the Packet process handle on server side
func (s *Server) process(sock *socket, p *Packet) {
nsp, ok := s.getnsp(p.Namespace)
if !ok {
if p.Type > PacketTypeDisconnect {
sock.emitError(p.Namespace, ErrorNamespaceUnavaialble.Error())
}
return
}
switch p.Type {
case PacketTypeConnect:
sock.attachnsp(p.Namespace)
if err := sock.emitPacket(&Packet{
Type: PacketTypeConnect,
Namespace: p.Namespace,
}); err != nil {
if nsp.onError != nil {
nsp.onError(&nspSock{socket: sock, name: p.Namespace}, err)
}
}
if nsp.onConnect != nil {
nsp.onConnect(&nspSock{socket: sock, name: p.Namespace})
}
case PacketTypeDisconnect:
sock.detachnsp(p.Namespace)
if nsp.onDisconnect != nil {
nsp.onDisconnect(&nspSock{socket: sock, name: p.Namespace})
}
case PacketTypeEvent, PacketTypeBinaryEvent:
event, data, bin, err := sock.decoder.ParseData(p)
if err != nil {
if nsp.onError != nil {
nsp.onError(&nspSock{socket: sock, name: p.Namespace}, err)
}
return
}
if event == "" {
return
}
v, err := nsp.fireEvent(&nspSock{socket: sock, name: p.Namespace}, event, data, bin, sock.decoder)
if err != nil {
if nsp.onError != nil {
nsp.onError(&nspSock{socket: sock, name: p.Namespace}, err)
}
return
}
if p.ID != nil {
p.Data = nil
if v != nil {
d := make([]interface{}, len(v))
for i := range d {
d[i] = v[i].Interface()
}
p.Data = d
}
if err = sock.ack(p); err != nil {
if nsp.onError != nil {
nsp.onError(&nspSock{socket: sock, name: p.Namespace}, err)
}
}
}
case PacketTypeAck, PacketTypeBinaryAck:
if p.ID != nil {
_, data, bin, err := sock.decoder.ParseData(p)
if err != nil {
if nsp.onError != nil {
nsp.onError(&nspSock{socket: sock, name: p.Namespace}, err)
}
return
}
sock.fireAck(p.Namespace, *p.ID, data, bin, sock.decoder)
}
case PacketTypeError:
if nsp.onError != nil {
nsp.onError(&nspSock{socket: sock, name: p.Namespace}, p.Data)
}
default:
if nsp.onError != nil {
nsp.onError(&nspSock{socket: sock, name: p.Namespace}, ErrUnknownPacket)
}
}
}
var (
WebsocketTransport = engine.WebsocketTransport
)