-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclient.go
160 lines (145 loc) · 3.97 KB
/
client.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
package socketio
import (
"net/http"
"github.com/zyxar/socketio/engine"
)
// Client is socket.io client
type Client struct {
engine *engine.Client
*socket
nsps map[string]*namespace
onError func(err interface{})
}
// NewClient creates a Client instance; use Dial to initialize underlying network
func NewClient() (c *Client) {
return &Client{nsps: make(map[string]*namespace)}
}
// Dial connects to a socket.io server represented by `rawurl` and create Client instance on success.
func (c *Client) Dial(rawurl string, requestHeader http.Header, dialer engine.Dialer, parser Parser) (err error) {
e, err := engine.Dial(rawurl, requestHeader, dialer)
if err != nil {
return
}
socket := newSocket(e.Socket, parser)
c.engine = e
c.socket = socket
e.On(engine.EventMessage, engine.Callback(func(_ *engine.Socket, msgType engine.MessageType, data []byte) {
switch msgType {
case engine.MessageTypeString:
case engine.MessageTypeBinary:
default:
return
}
if err := socket.decoder.Add(msgType, data); err != nil {
if c.onError != nil {
c.onError(err)
}
}
if p := socket.yield(); p != nil {
c.process(socket, p)
}
}))
e.On(engine.EventClose, engine.Callback(func(_ *engine.Socket, _ engine.MessageType, _ []byte) {
socket.Close()
detachall(c, socket)
}))
return
}
// Emit send event messages to namespace `nsp`
func (c *Client) Emit(nsp string, event string, args ...interface{}) (err error) {
return c.socket.emit(nsp, event, args...)
}
// Sid returns session id assigned by socket.io server
func (c *Client) Sid() string {
return c.engine.Sid()
}
// Close closes underlying engine.io transport
func (c *Client) Close() error {
return c.engine.Close()
}
// OnError registers fn as error callback
func (c *Client) OnError(fn func(interface{})) {
c.onError = fn
}
// Namespace ensures a Namespace instance exists in client
func (c *Client) Namespace(nsp string) Namespace { return c.creatensp(nsp) }
func (c *Client) creatensp(nsp string) *namespace {
n, ok := c.nsps[nsp]
if !ok {
n = &namespace{callbacks: make(map[string]*callback)}
c.nsps[nsp] = n
}
return n
}
func (c *Client) getnsp(nsp string) (n *namespace, ok bool) { n, ok = c.nsps[nsp]; return }
// process is the Packet process handle on client side
func (c *Client) process(sock *socket, p *Packet) {
nsp, ok := c.getnsp(p.Namespace)
if !ok {
return
}
switch p.Type {
case PacketTypeConnect:
sock.attachnsp(p.Namespace)
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)
}
}
}