forked from googollee/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
197 lines (178 loc) · 4.11 KB
/
handler.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
package socketio
import (
"fmt"
"reflect"
)
type baseHandler struct {
events map[string]*caller
name string
broadcast BroadcastAdaptor
}
func newBaseHandler(name string, broadcast BroadcastAdaptor) *baseHandler {
return &baseHandler{
events: make(map[string]*caller),
name: name,
broadcast: broadcast,
}
}
// On registers the function f to handle an event.
func (h *baseHandler) On(event string, f interface{}) error {
c, err := newCaller(f)
if err != nil {
return err
}
h.events[event] = c
return nil
}
type socketHandler struct {
*baseHandler
acks map[int]*caller
socket *socket
rooms map[string]struct{}
}
func newSocketHandler(s *socket, base *baseHandler) *socketHandler {
events := make(map[string]*caller)
for k, v := range base.events {
events[k] = v
}
return &socketHandler{
baseHandler: &baseHandler{
events: events,
broadcast: base.broadcast,
},
acks: make(map[int]*caller),
socket: s,
rooms: make(map[string]struct{}),
}
}
func (h *socketHandler) Emit(event string, args ...interface{}) error {
var c *caller
if l := len(args); l > 0 {
fv := reflect.ValueOf(args[l-1])
if fv.Kind() == reflect.Func {
var err error
c, err = newCaller(args[l-1])
if err != nil {
return err
}
args = args[:l-1]
}
}
args = append([]interface{}{event}, args...)
if c != nil {
id, err := h.socket.sendId(args)
if err != nil {
return err
}
h.acks[id] = c
return nil
}
return h.socket.send(args)
}
func (h *socketHandler) Rooms() []string {
ret := make([]string, len(h.rooms))
i := 0
for room := range h.rooms {
ret[i] = room
i++
}
return ret
}
func (h *socketHandler) Join(room string) error {
if err := h.baseHandler.broadcast.Join(h.broadcastName(room), h.socket); err != nil {
return err
}
h.rooms[room] = struct{}{}
return nil
}
func (h *socketHandler) Leave(room string) error {
if err := h.baseHandler.broadcast.Leave(h.broadcastName(room), h.socket); err != nil {
return err
}
delete(h.rooms, room)
return nil
}
func (h *socketHandler) LeaveAll() error {
for room := range h.rooms {
if err := h.baseHandler.broadcast.Leave(h.broadcastName(room), h.socket); err != nil {
return err
}
}
return nil
}
func (h *baseHandler) BroadcastTo(room, event string, args ...interface{}) error {
return h.broadcast.Send(nil, h.broadcastName(room), event, args...)
}
func (h *socketHandler) BroadcastTo(room, event string, args ...interface{}) error {
return h.baseHandler.broadcast.Send(h.socket, h.broadcastName(room), event, args...)
}
func (h *baseHandler) broadcastName(room string) string {
return fmt.Sprintf("%s:%s", h.name, room)
}
func (h *socketHandler) onPacket(decoder *decoder, packet *packet) ([]interface{}, error) {
var message string
switch packet.Type {
case _CONNECT:
message = "connection"
case _DISCONNECT:
message = "disconnection"
case _ERROR:
message = "error"
case _ACK:
fallthrough
case _BINARY_ACK:
return nil, h.onAck(packet.Id, decoder, packet)
default:
if decoder != nil {
message = decoder.Message()
}
}
c, ok := h.events[message]
if !ok {
// If the message is not recognized by the server, the decoder.currentCloser
// needs to be closed otherwise the server will be stuck until the e
if decoder != nil {
decoder.Close()
}
return nil, nil
}
args := c.GetArgs()
olen := len(args)
if olen > 0 && decoder != nil {
packet.Data = &args
if err := decoder.DecodeData(packet); err != nil {
return nil, err
}
}
for i := len(args); i < olen; i++ {
args = append(args, nil)
}
retV := c.Call(h.socket, args)
if len(retV) == 0 {
return nil, nil
}
var err error
if last, ok := retV[len(retV)-1].Interface().(error); ok {
err = last
retV = retV[0 : len(retV)-1]
}
ret := make([]interface{}, len(retV))
for i, v := range retV {
ret[i] = v.Interface()
}
return ret, err
}
func (h *socketHandler) onAck(id int, decoder *decoder, packet *packet) error {
c, ok := h.acks[id]
if !ok {
return nil
}
delete(h.acks, id)
args := c.GetArgs()
packet.Data = &args
if err := decoder.DecodeData(packet); err != nil {
return err
}
c.Call(h.socket, args)
return nil
}