2012-05-07 14:41:15 +02:00
|
|
|
;;; ein-websocket.el --- Wrapper of websocket.el
|
|
|
|
|
|
|
|
;; Copyright (C) 2012- Takafumi Arakaki
|
|
|
|
|
2012-07-01 20:18:05 +02:00
|
|
|
;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
|
2012-05-07 14:41:15 +02:00
|
|
|
|
|
|
|
;; This file is NOT part of GNU Emacs.
|
|
|
|
|
|
|
|
;; ein-websocket.el is free software: you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; ein-websocket.el is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with ein-websocket.el. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(eval-when-compile (require 'cl))
|
|
|
|
(require 'websocket)
|
|
|
|
|
2012-08-28 15:26:32 +02:00
|
|
|
(require 'ein-core)
|
2012-05-07 14:41:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
(defstruct ein:$websocket
|
2012-05-13 16:13:52 +02:00
|
|
|
"A wrapper object of `websocket'.
|
|
|
|
|
|
|
|
`ein:$websocket-ws' : an instance returned by `websocket-open'
|
|
|
|
|
|
|
|
`ein:$websocket-onmessage' : function called with (PACKET &rest ARGS)'
|
|
|
|
`ein:$websocket-onclose' : function called with (WEBSOCKET &rest ARGS)'
|
|
|
|
`ein:$websocket-onopen' : function called with (&rest ARGS)'
|
|
|
|
|
|
|
|
`ein:$websocket-onmessage-args' : optional arguments for onmessage callback'
|
|
|
|
`ein:$websocket-onclose-args' : optional arguments for onclose callback'
|
|
|
|
`ein:$websocket-onopen-args' : optional arguments for onopen callback'
|
|
|
|
|
|
|
|
`ein:$websocket-closed-by-client' : t/nil'
|
|
|
|
"
|
2012-05-07 14:41:15 +02:00
|
|
|
ws
|
2012-05-13 16:13:52 +02:00
|
|
|
onmessage
|
|
|
|
onmessage-args
|
|
|
|
onclose
|
|
|
|
onclose-args
|
|
|
|
onopen
|
|
|
|
onopen-args
|
|
|
|
closed-by-client)
|
2012-05-07 14:41:15 +02:00
|
|
|
|
|
|
|
|
2014-04-14 12:48:55 -05:00
|
|
|
;; Issues opening websockets in IPython 2.0, think it is related to
|
|
|
|
;; http://stackoverflow.com/questions/22202182/error-on-websocket-when-try-to-use-ipython-notebook-in-emacs
|
2012-05-09 08:15:05 +02:00
|
|
|
(defun ein:websocket (url &optional onmessage onclose onopen
|
|
|
|
onmessage-args onclose-args onopen-args)
|
2012-05-07 14:41:15 +02:00
|
|
|
(let ((websocket (make-ein:$websocket
|
|
|
|
:onmessage onmessage
|
|
|
|
:onclose onclose
|
2012-05-09 08:15:05 +02:00
|
|
|
:onopen onopen
|
|
|
|
:onmessage-args onmessage-args
|
|
|
|
:onclose-args onclose-args
|
2012-06-01 22:11:34 +02:00
|
|
|
:onopen-args onopen-args))
|
|
|
|
(ws (websocket-open
|
2012-05-07 14:41:15 +02:00
|
|
|
url
|
2012-06-01 22:11:34 +02:00
|
|
|
:on-open
|
|
|
|
(lambda (ws)
|
|
|
|
(let ((websocket (websocket-client-data ws)))
|
|
|
|
(ein:aif (ein:$websocket-onopen websocket)
|
2012-06-01 22:56:09 +02:00
|
|
|
(apply it (ein:$websocket-onopen-args websocket)))))
|
2012-06-01 22:11:34 +02:00
|
|
|
:on-message
|
|
|
|
(lambda (ws frame)
|
2012-06-01 23:23:58 +02:00
|
|
|
(let ((websocket (websocket-client-data ws))
|
|
|
|
(packet (websocket-frame-payload frame)))
|
|
|
|
(ein:aif (ein:$websocket-onmessage websocket)
|
|
|
|
(when packet
|
|
|
|
(apply it packet
|
|
|
|
(ein:$websocket-onmessage-args websocket))))))
|
2012-06-01 22:11:34 +02:00
|
|
|
:on-close
|
|
|
|
(lambda (ws)
|
2012-06-01 23:23:58 +02:00
|
|
|
(let ((websocket (websocket-client-data ws)))
|
|
|
|
(ein:aif (ein:$websocket-onclose websocket)
|
|
|
|
(apply it websocket
|
|
|
|
(ein:$websocket-onclose-args websocket))))))))
|
2012-06-01 22:11:34 +02:00
|
|
|
(setf (websocket-client-data ws) websocket)
|
|
|
|
(setf (ein:$websocket-ws websocket) ws)
|
2012-05-07 14:41:15 +02:00
|
|
|
websocket))
|
|
|
|
|
|
|
|
|
2012-05-13 16:23:57 +02:00
|
|
|
(defun ein:websocket-open-p (websocket)
|
2012-06-01 22:56:09 +02:00
|
|
|
(eql (websocket-ready-state (ein:$websocket-ws websocket)) 'open))
|
2012-05-13 16:23:57 +02:00
|
|
|
|
|
|
|
|
2012-05-07 14:41:15 +02:00
|
|
|
(defun ein:websocket-send (websocket text)
|
2014-10-22 21:35:20 -05:00
|
|
|
;; (ein:log 'info "WS: Sent message %s" text)
|
2012-06-01 22:11:34 +02:00
|
|
|
(websocket-send-text (ein:$websocket-ws websocket) text))
|
2012-05-07 14:41:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
(defun ein:websocket-close (websocket)
|
2012-05-09 08:54:02 +02:00
|
|
|
(setf (ein:$websocket-closed-by-client websocket) t)
|
2012-05-07 14:41:15 +02:00
|
|
|
(websocket-close (ein:$websocket-ws websocket)))
|
|
|
|
|
|
|
|
|
|
|
|
(provide 'ein-websocket)
|
|
|
|
|
|
|
|
;;; ein-websocket.el ends here
|