Add ein:$websocket-*-args for callbacks

This commit is contained in:
Takafumi Arakaki 2012-05-09 08:15:05 +02:00
parent 988e43138c
commit 9b2bcf5a00

View file

@ -33,16 +33,24 @@
(defstruct ein:$websocket (defstruct ein:$websocket
ws ws
onmessage onmessage ; function called with (PACKET &rest ARGS)
onclose onmessage-args ; optional arguments for onmessage callback
onopen) onclose ; function called with (WEBSOCKET &rest ARGS)
onclose-args ; optional arguments for onclose callback
onopen ; function called with (&rest ARGS)
onopen-args) ; optional arguments for onopen callback
;; FIXME: probably, first arguments of any callback must be WEBSOCKET.
(defun ein:websocket (url &optional onmessage onclose onopen) (defun ein:websocket (url &optional onmessage onclose onopen
onmessage-args onclose-args onopen-args)
(let ((websocket (make-ein:$websocket (let ((websocket (make-ein:$websocket
:onmessage onmessage :onmessage onmessage
:onclose onclose :onclose onclose
:onopen onopen))) :onopen onopen
:onmessage-args onmessage-args
:onclose-args onclose-args
:onopen-args onopen-args)))
(setf (ein:$websocket-ws websocket) (setf (ein:$websocket-ws websocket)
(lexical-let ((websocket websocket)) (lexical-let ((websocket websocket))
(websocket-open (websocket-open
@ -52,7 +60,8 @@
;; Pseudo onopen callback. Until websocket.el supports it. ;; Pseudo onopen callback. Until websocket.el supports it.
(run-at-time 1 nil (run-at-time 1 nil
(lambda (ws) (lambda (ws)
(ein:aif (ein:$websocket-onopen ws) (funcall it))) (ein:aif (ein:$websocket-onopen ws)
(apply it (ein:$websocket-onopen-args ws))))
websocket) websocket)
websocket)) websocket))
@ -67,12 +76,12 @@
(defun ein:websocket-filter (websocket packet) (defun ein:websocket-filter (websocket packet)
(ein:aif (ein:$websocket-onmessage websocket) (ein:aif (ein:$websocket-onmessage websocket)
(funcall it packet))) (apply it packet (ein:$websocket-onmessage-args websocket))))
(defun ein:websocket-onclose (websocket) (defun ein:websocket-onclose (websocket)
(ein:aif (ein:$websocket-onclose websocket) (ein:aif (ein:$websocket-onclose websocket)
(funcall it websocket))) (apply it websocket (ein:$websocket-onclose-args websocket))))
(provide 'ein-websocket) (provide 'ein-websocket)