Note: reconnecting-websocket is a Common Lisp fork of reconnecting-websocket in JavaScript, base on websocket-driver.
In Shell:
git clone https://github.com/muyinliu/reconnecting-websocket.git
cp -r reconnecting-websocket ~/quicklisp/local-projects/reconnecting-websocket
Then in Common Lisp REPL(require QuickLisp):
(ql:quickload 'reconnecting-websocket)
=>
To load "reconnecting-websocket":
Load 1 ASDF system:
reconnecting-websocket
; Loading "reconnecting-websocket"
.....
(RECONNECTING-WEBSOCKET)
Note: the nickname of package reconnecting-websocket
is rws
- websocket-driver-client A WebSocket Client
- babel Encode/Decode String/Bytes
- websocket-driver A WebSocket Server
- prove A test system
(defvar *rws-event-listeners*
(list :open (list #'(lambda (client)
(declare (ignorable client))
(format t "onopen~%")))
:message (list #'(lambda (client message)
(declare (ignorable client))
(format t "onmessage: ~S~%"
(babel:octets-to-string message :encoding :utf-8))))
:close (list #'(lambda (client &key code reason)
(declare (ignorable client))
(format t "onclose: code: ~S, reason: ~S~%"
code reason)))
:error (list #'(lambda (client error)
(declare (ignorable client))
(format t "onerror: ~S~%" error)))))
(defvar *rws-client* (make-instance 'rws:reconnecting-websocket
:url "ws://localhost/"
:event-listeners *rws-event-listeners*))
(rws:send *rws-client* (babel:string-to-octets "message send to server" :encoding :utf-8))
;; bind more listener
(rws:on *rws-client*
:open #'(lambda (client)
(declare (ignorable client))
(format t "onopen event~%")))
Note: rws
stands for Reconnecting WebSocket
The base class for auto-reconnect WebSocket client, a wrapper of websocket-driver-client
[Function] (rws:make-reconnecting-websocket url &key debug-p auto-open-p reconnect-interval max-reconnect-interval reconnect-decay max-reconnect-attempts event-listeners)
Create an auto-reconnect WebSocket client.
The same event as websocket-driver-client's
Bind event handler to auto-reconnect WebSocket client
(rws:on rws-client event handler)
for example:
(rws:on rws-client
:open
#'(lambda (client)
(format t "onopen: ~S~%" client)))
(rws:on rws-client
:message
#'(lambda (client message)
(format t "onmessage: ~S, ~S~%"
client message)))
(rws:on rws-client
:error
#'(lambda (client error)
(format t "onerror:~S, ~S~%"
client error)))
(rws:on rws-client
:close
#'(lambda (client &key code reason)
(format t "onclose: ~S, code: ~S, reason: ~S~%"
client code reason)))
Remove event listener from auto-reconnect WebSocket client
Remove all event listeners from auto-reconnect WebSocket client
Manually start a WebSocket connection.
Note: by default reconnecting-websocket auto start-connection without option :auto-open-p nil
Send message to WebSocket Server
Send text(String) message to WebSocket Server
Send binary(bytes) message to WebSocket Server
Send ping message to WebSocket Server
Manually close a WebSocket connection and will NOT auto-reconnect again.
Get URL of auto-reconnect WebSocket client
Note: Readonly
Trun on/off debug message output to *debug-io*
Trun on debug message output:
(setf (rws:debug-p rws-client) t)
Whether to auto-open connection while auto-reconnect WebSocket client is creating
Note: Default t
Delay before reconnecting WebSocket client
Note: Default 1
(second)
Max delay before reconnecting WebSocket client
Default: 30(s)
Decay rate between one reconnection and next reconnection, for example: current delay before reconnecting is 1 second, then next delay before reconnecting is 1.5 second, the next next delay before reconnecting is 2.25 second...
Default: 1.5
Default: nil, reconnect forever
Return origin websocket-driver-client
Return a property list like this:
'(:open (#'(lambda () xxx))
:message (#'(lambda () yyy))
:close (#'(lambda () zzz))
:error (#'(lambda () aaa)))
Return WebSocket client's current ready-state, one of :connecting
:open
:closed
(setf (rws:debug-p *rws-client*) t)
Note: All debug message will output to *debug-io*
In Common Lisp REPL:
(asdf:test-system :reconnecting-websocket)
Or in Shell:
sbcl --eval "(asdf:test-system :reconnecting-websocket)" --eval "(quit)"
MIT (See LICENSE file for details).