-
Notifications
You must be signed in to change notification settings - Fork 0
/
reconnecting-websocket.lisp
280 lines (266 loc) · 10.6 KB
/
reconnecting-websocket.lisp
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
(defpackage reconnecting-websocket
(:use :cl)
(:nicknames :rws)
(:export #:reconnecting-websocket
#:make-reconnecting-websocket
#:url
#:debug-p
#:auto-open-p
#:reconnect-interval
#:max-reconnect-interval
#:reconnect-decay
#:max-reconnect-attempts
#:reconnect-attempts
#:client
#:event-listeners
#:ready-state
#:start-connection
#:close-connection
#:on
#:remove-listener
#:remove-all-listeners
#:send
#:send-text
#:send-binary
#:send-ping))
(in-package :reconnecting-websocket)
(defclass reconnecting-websocket ()
((url
:initarg :url
:reader url
:initform (error "url is required"))
(debug-p
:initarg :debug-p
:accessor debug-p
:initform nil)
(auto-open-p
:initarg :auto-open-p
:accessor auto-open-p
:initform t)
(reconnect-interval
:initarg :reconnect-interval
:accessor reconnect-interval
:initform 1)
(max-reconnect-interval
:initarg :max-reconnect-interval
:accessor max-reconnect-interval
:initform 30)
(reconnect-decay
:initarg :reconnect-decay
:accessor reconnect-decay
:initform 1.5)
(max-reconnect-attempts
:initarg :max-reconnect-attempts
:accessor max-reconnect-attempts
:initform nil)
(reconnect-attempts
:accessor reconnect-attempts
:initform 0)
(client
:accessor client
:initform nil)
(forced-close-p
:accessor forced-close-p
:initform nil)
(event-listeners
:initarg :event-listeners
:accessor event-listeners
:initform nil)
(ready-state
:accessor ready-state
:initform :connecting)))
(defun make-reconnecting-websocket (url
&key
(debug-p nil)
(auto-open-p t)
(reconnect-interval 1)
(max-reconnect-interval 30)
(reconnect-decay 1.5)
max-reconnect-attempts
event-listeners)
(make-instance 'reconnecting-websocket
:url url
:debug-p debug-p
:auto-open-p auto-open-p
:reconnect-interval reconnect-interval
:max-reconnect-interval max-reconnect-interval
:reconnect-decay reconnect-decay
:max-reconnect-attempts max-reconnect-attempts
:event-listeners event-listeners))
(defmethod initialize-instance :after ((client reconnecting-websocket) &key)
(when (auto-open-p client)
(start-connection client)))
(defmethod close-connection ((client reconnecting-websocket) &optional reason code)
(unless code
(setf code 1000))
(setf (forced-close-p client) t)
(when (client client)
(if (eq :open (ready-state client))
(wsd:close-connection (client client) reason code)
(setf (ready-state client):closed))))
(defmethod start-connection ((client reconnecting-websocket) &optional reconnect-attempt-p)
(when (client client)
(ignore-errors
(wsd:close-connection (client client))))
(setf (client client)
(wsd:make-client (url client)))
(if reconnect-attempt-p
(when (and (max-reconnect-attempts client)
(> (reconnect-attempts client)
(max-reconnect-attempts client)))
(let ((reason (format nil
"ReconnectingWebSocket attempt-connect too many times: ~A~%"
(reconnect-attempts client))))
(when (debug-p client)
(format *debug-io* "~A~%" reason))
(close-connection client reason)
(dolist (listener (getf (event-listeners client) :error))
(funcall listener client reason)))
(return-from start-connection))
(progn
;; eventTarget.dispatchEvent(generateEvent('connecting'));
(setf (reconnect-attempts client) 0)))
(when (debug-p client)
(format *debug-io* "ReconnectingWebSocket attempt-connect ~S ~A times~%"
(url client)
(reconnect-attempts client)))
(wsd:on :open (client client)
#'(lambda ()
(when (debug-p client)
(format *debug-io* "ReconnectingWebSocket onopen ~S~%" (url client)))
(setf (ready-state client) :open)
(setf (reconnect-attempts client) 0)
(setf reconnect-attempt-p nil)
;; dispatch :open event
(dolist (listener (getf (event-listeners client) :open))
(funcall listener client))))
(wsd:on :close (client client)
#'(lambda (&key code reason)
(setf (client client) nil)
(if (forced-close-p client)
(progn
(setf (ready-state client) :closed)
;; dispatch :closed event
(dolist (listener (getf (event-listeners client) :close))
(funcall listener client :code code :reason reason)))
(progn
(setf (ready-state client) :connecting)
(when (not reconnect-attempt-p)
(when (debug-p client)
(format *debug-io*
"ReconnectingWebSocket onclose ~S, code: ~S, reason: ~S~%"
(url client)
code
reason))
;; dispatch :close event
(dolist (listener (getf (event-listeners client) :close))
(funcall listener client :code code :reason reason)))
(let* ((interval (* (reconnect-interval client)
(expt (reconnect-decay client)
(reconnect-attempts client))))
(interval (if (> interval (max-reconnect-interval client))
(max-reconnect-interval client)
interval)))
(when (debug-p client)
(format *debug-io*
"ReconnectingWebSocket ~S will wait ~As~%"
(url client)
interval))
(sleep interval)
(incf (reconnect-attempts client))
(start-connection client t))))))
(wsd:on :message (client client)
#'(lambda (message)
(when (debug-p client)
(format *debug-io*
"ReconnectingWebSocket onmessage ~S, ~S~%"
(url client)
message))
;; dispatch :message event
(dolist (listener (getf (event-listeners client) :message))
(funcall listener client message))))
(wsd:on :error (client client)
#'(lambda (error)
(when (debug-p client)
(format *debug-io* "ReconnectingWebSocket onerror ~S, ~S~%"
(url client)
error))
;; dispatch :error event
(dolist (listener (getf (event-listeners client) :error))
(funcall listener client error))))
(handler-case
(wsd:start-connection (client client))
(error (condition)
(when (debug-p client)
(format *debug-io* "ReconnectingWebSocket start-connection error ~S~%"
condition))
(event-emitter:emit :close (client client)))))
(defmethod on ((client reconnecting-websocket) event handler)
(pushnew handler (getf (event-listeners client) event)))
(defmethod remove-listener ((client reconnecting-websocket) event handler)
(wsd:remove-listener (client client) event handler)
(setf (getf (event-listeners client) event)
(remove handler (getf (event-listeners client) event))))
(defmethod remove-all-listeners ((client reconnecting-websocket) &optional event)
(if event
(progn
(wsd:remove-all-listeners (client client) event)
(setf (event-listeners client)
(remf (event-listeners client) event)))
(progn
(wsd:remove-all-listeners (client client))
(setf (event-listeners client) nil))))
(defmethod send ((client reconnecting-websocket) data &key start end type code callback)
(if (and (client client)
(equal :open (ready-state client)))
(progn
(when (debug-p client)
(format *debug-io* "ReconnectingWebSocket send ~S ~S~%"
(url client)
data))
(wsd:send (client client)
data
:start start
:end end
:type type
:code code
:callback callback))
(error "INVALID_STATE_ERR : Pausing to reconnect websocket")))
(defmethod send-text ((client reconnecting-websocket) message &key start end callback)
(if (and (client client)
(equal :open (ready-state client)))
(progn
(when (debug-p client)
(format *debug-io* "ReconnectingWebSocket send-text ~S ~S~%"
(url client)
message))
(wsd:send-text (client client)
message
:start start
:end end
:callback callback))
(error "INVALID_STATE_ERR : Pausing to reconnect websocket")))
(defmethod send-binary ((client reconnecting-websocket) usb8-vector &key start end callback)
(if (and (client client)
(equal :open (ready-state client)))
(progn
(when (debug-p client)
(format *debug-io* "ReconnectingWebSocket send-binary ~S ~S~%"
(url client)
usb8-vector))
(wsd:send-binary (client client)
usb8-vector
:start start
:end end
:callback callback))
(error "INVALID_STATE_ERR : Pausing to reconnect websocket")))
(defmethod send-ping ((client reconnecting-websocket) &optional message callback)
(if (and (client client)
(equal :open (ready-state client)))
(progn
(when (debug-p client)
(format *debug-io* "ReconnectingWebSocket send-ping ~S ~S~%"
(url client)
message))
(wsd:send-ping (client client) message callback))
(error "INVALID_STATE_ERR : Pausing to reconnect websocket")))