forked from leviathan-1776/socketio-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
209 lines (193 loc) · 4.29 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
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
package socketio
import (
"math"
"net/http"
"net/url"
"sync/atomic"
"time"
"github.com/leviathan-1776/socketio-client-go/protocol"
)
const (
stateOpen uint32 = iota
stateConnecting
stateReady
stateReconnecting
stateClose
)
type option struct {
AutoReconnect bool
MaxReconnections int32
}
var defaultOption = &option{
AutoReconnect: true,
MaxReconnections: math.MaxInt32,
}
type SocketClient struct {
emitter
state uint32
url *url.URL
option *option
transport protocol.Transport
outChan chan *protocol.Packet
closeChan chan bool
}
func Socket(urlString string) (*SocketClient, error) {
u, err := url.Parse(urlString)
if err != nil {
return nil, err
}
u.Path = "/socket.io/"
q := u.Query()
q.Add("EIO", "3")
q.Add("transport", "websocket")
u.RawQuery = q.Encode()
return &SocketClient{
emitter: emitter{listeners: make(map[string][]Listener)},
url: u,
option: defaultOption,
transport: protocol.NewWebSocketTransport(),
outChan: make(chan *protocol.Packet, 64),
closeChan: make(chan bool),
}, nil
}
func (s *SocketClient) GetUrl() string {
return s.url.String()
}
func (s *SocketClient) Connect(requestHeader http.Header) {
if atomic.CompareAndSwapUint32(&s.state, stateOpen, stateConnecting) {
conn, err := s.transport.Dial(s.url.String(), requestHeader)
if err != nil {
s.emit(EventError, err)
go s.reconnect(stateConnecting, requestHeader)
return
}
if atomic.CompareAndSwapUint32(&s.state, stateConnecting, stateReady) {
go s.start(conn, requestHeader)
s.emit(EventConnect)
} else {
_ = conn.Close()
}
}
}
func (s *SocketClient) Disconnect() {
atomic.StoreUint32(&s.state, stateClose)
close(s.outChan)
close(s.closeChan)
}
func (s *SocketClient) Emit(event string, args ...interface{}) {
if atomic.LoadUint32(&s.state) == stateReady && !s.emit(event, args) {
m := &protocol.Message{
Type: protocol.MessageTypeEvent,
Namespace: "/",
ID: -1,
Event: event,
Payloads: args,
}
p, err := m.Encode()
if err != nil {
s.emit(EventError, err)
} else {
s.outChan <- p
}
}
}
func (s *SocketClient) reconnect(state uint32, requestHeader http.Header) {
time.Sleep(time.Second)
if atomic.CompareAndSwapUint32(&s.state, state, stateReconnecting) {
conn, err := s.transport.Dial(s.url.String(), requestHeader)
if err != nil {
s.emit(EventError, err)
go s.reconnect(stateReconnecting, requestHeader)
return
}
if atomic.CompareAndSwapUint32(&s.state, stateReconnecting, stateReady) {
go s.start(conn, requestHeader)
s.emit(EventReconnect)
} else {
_ = conn.Close()
}
}
}
func (s *SocketClient) start(conn protocol.Conn, requestHeader http.Header) {
stopper := make(chan bool)
go s.startRead(conn, stopper)
go s.startWrite(conn, stopper)
select {
case <-stopper:
go s.reconnect(stateReady, requestHeader)
_ = conn.Close()
case <-s.closeChan:
_ = conn.Close()
}
}
func (s *SocketClient) startRead(conn protocol.Conn, stopper chan bool) {
defer func() {
recover()
}()
for atomic.LoadUint32(&s.state) == stateReady {
p, err := conn.Read()
if err != nil {
s.emit(EventError, err)
close(stopper)
return
}
switch p.Type {
case protocol.PacketTypeOpen:
h, err := p.DecodeHandshake()
if err != nil {
s.emit(EventError, err)
} else {
go s.startPing(h, stopper)
}
case protocol.PacketTypePing:
s.outChan <- protocol.NewPongPacket()
case protocol.PacketTypeMessage:
m, err := p.DecodeMessage()
if err != nil {
s.emit(EventError, err)
} else {
s.emit(m.Event, m.Payloads...)
}
}
}
}
func (s *SocketClient) startWrite(conn protocol.Conn, stopper chan bool) {
defer func() {
recover()
}()
for atomic.LoadUint32(&s.state) == stateReady {
select {
case <-stopper:
return
case p, ok := <-s.outChan:
if !ok {
return
}
err := conn.Write(p)
if err != nil {
s.emit(EventError, err)
close(stopper)
return
}
}
}
}
func (s *SocketClient) startPing(h *protocol.Handshake, stopper chan bool) {
defer func() {
recover()
}()
for {
time.Sleep(time.Duration(h.PingInterval) * time.Millisecond)
select {
case <-stopper:
return
case <-s.closeChan:
return
default:
}
if atomic.LoadUint32(&s.state) != stateReady {
return
}
s.outChan <- protocol.NewPingPacket()
}
}