[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] 81/119: implemented ws-web-socket-frame to send replies
From: |
Eric Schulte |
Subject: |
[elpa] 81/119: implemented ws-web-socket-frame to send replies |
Date: |
Mon, 10 Mar 2014 16:57:44 +0000 |
eschulte pushed a commit to branch master
in repository elpa.
commit b1b6d5cec88858bf4fad516f01b36d97e7b26245
Author: Eric Schulte <address@hidden>
Date: Tue Jan 7 12:32:42 2014 -0700
implemented ws-web-socket-frame to send replies
something is not quite right however as the browser (Firefox doesn't
like the replies and complains the connection is closing)
---
examples/9-web-socket.el | 20 ++++++++++++++++----
web-server.el | 21 +++++++++++++++++----
2 files changed, 33 insertions(+), 8 deletions(-)
diff --git a/examples/9-web-socket.el b/examples/9-web-socket.el
index 1ad9276..1a2e4fb 100644
--- a/examples/9-web-socket.el
+++ b/examples/9-web-socket.el
@@ -1,6 +1,6 @@
;;; web-sockets.el --- communicate via web-sockets
-(defvar web-socket-port 8888)
+(defvar web-socket-port 7777)
(defvar web-socket-page
(format "<html>
@@ -27,10 +27,22 @@ function close(){ ws.close(); };
</body>
</html>" web-socket-port))
+(defvar my-connection nil)
+
(defun web-socket-server (request)
(with-slots (process headers) request
- (ws-web-socket-connect request 'ws-web-socket-send)
- (ws-response-header process 200 '("Content-type" . "text/html"))
- (process-send-string process web-socket-page)))
+ ;; if a web-socket request, then connect and keep open
+ (if (ws-web-socket-connect request
+ (lambda (proc string)
+ (message "received:%S" string)
+ (let ((reply (ws-web-socket-frame (concat "echo: " string))))
+ (message "sending:%S" reply)
+ (process-send-string proc reply)
+ (sit-for 5))
+ :keep-alive))
+ (prog1 :keep-alive (setq my-connection process))
+ ;; otherwise send the index page
+ (ws-response-header process 200 '("Content-type" . "text/html"))
+ (process-send-string process web-socket-page))))
(ws-start '(((:GET . ".*") . web-socket-server)) web-socket-port)
diff --git a/web-server.el b/web-server.el
index b0cbfa6..35c09bf 100644
--- a/web-server.el
+++ b/web-server.el
@@ -339,7 +339,7 @@ received and parsed from the network."
process (list :message (make-instance 'ws-message
:handler handler :process process)))
(set-process-filter process 'ws-web-socket-filter)
- (throw 'close-connection :keep-alive))))
+ process)))
(defun ws-web-socket-filter (process string)
(let ((message (plist-get (process-plist process) :message)))
@@ -445,15 +445,28 @@ See RFC6455."
;; wipe the message state and call the handler
(let ((it data))
(setq data "" active nil pending "" new nil)
- (funcall handler it))
+ (funcall handler process it))
;; add any remaining un-parsed network data to pending
(when (< (+ index pl) (length pending))
(setq pending (substring pending (+ index pl)))))))
;; possibly re-parse any pending input
(when (new message) (ws-web-socket-parse-messages message)))))
-(defun ws-web-socket-send (string)
- (message "TODO: send %S" string))
+(defun ws-web-socket-frame (string &optional opcode)
+ "Frame STRING for web socket communication."
+ (let* ((fin 1) ;; set to 0 if not final frame
+ (len (length string))
+ (pl (cond ((< len 126) len)
+ ((< len (expt 2 16)) 126)
+ (t (ws-error process "TODO: messages of length %d" len))))
+ (opcode (ecase (or opcode :TEXT) (:TEXT 1) (:BINARY 2))))
+ ;; for now we won't do any masking, as it isn't required. We'll
+ ;; also leave the rsv{1,2,3} flags all set to 0.
+ (format "%c%c%s%s"
+ (logior (lsh fin 7) opcode)
+ pl
+ (if (= pl 126) (logand (lsh v -8) 255) "")
+ string)))
;;; Convenience functions to write responses
- [elpa] 69/119: refactoring no multiple concurrent header parsing, (continued)
- [elpa] 69/119: refactoring no multiple concurrent header parsing, Eric Schulte, 2014/03/10
- [elpa] 71/119: updating notes and README text, Eric Schulte, 2014/03/10
- [elpa] 72/119: notes for running behind an Apache HTTPS proxy, Eric Schulte, 2014/03/10
- [elpa] 73/119: some more examples to implement, Eric Schulte, 2014/03/10
- [elpa] 68/119: no multiple concurrent entry of ws-parse-request, Eric Schulte, 2014/03/10
- [elpa] 76/119: and more web-socket progress, Eric Schulte, 2014/03/10
- [elpa] 74/119: beginning to implement web-socket support, Eric Schulte, 2014/03/10
- [elpa] 77/119: handle chunked receipt of web-socket messages, Eric Schulte, 2014/03/10
- [elpa] 78/119: hold, Eric Schulte, 2014/03/10
- [elpa] 79/119: beginning to add convenience macro for web sockets, Eric Schulte, 2014/03/10
- [elpa] 81/119: implemented ws-web-socket-frame to send replies,
Eric Schulte <=
- [elpa] 80/119: helpers for handling web socket connections, Eric Schulte, 2014/03/10
- [elpa] 82/119: web-sockets are working, Eric Schulte, 2014/03/10
- [elpa] 84/119: more examples, Eric Schulte, 2014/03/10
- [elpa] 85/119: renaming example files, Eric Schulte, 2014/03/10
- [elpa] 86/119: another example idea -- org export service, Eric Schulte, 2014/03/10
- [elpa] 87/119: update server stopping w/requests process field, Eric Schulte, 2014/03/10
- [elpa] 83/119: supports web sockets, Eric Schulte, 2014/03/10
- [elpa] 75/119: more web-socket implementation, Eric Schulte, 2014/03/10
- [elpa] 91/119: more tutorial, Eric Schulte, 2014/03/10
- [elpa] 94/119: example serving Org-mode files as JSON, Eric Schulte, 2014/03/10