Next: Chunked Transfer Encoding, Previous: File Upload, Up: Usage Examples [Contents][Index]
Example demonstrating the use of web sockets for full duplex
communication between clients and the server. Handlers may use the
ws-web-socket-connect function (see ws-web-socket-connect)
to check for and respond to a web socket upgrade request sent by the
client (as demonstrated with the new WebSocket JavaScript code
in the example). Upon successfully initializing a web socket
connection the call to ws-web-socket-connect will return the
web socket network process. This process may then be used by the
server to communicate with the client over the web socket using the
process-send-string and ws-web-socket-frame functions.
All web socket communication must be wrapped in frames using the
ws-web-socket-frame function.
The handler must pass a function as the second argument to
ws-web-socket-connect. This function will be called on every
web socket message received from the client.
Note: in order to keep the web socket connection alive the request
handler from which ws-web-socket-connect is called must return
the :keep-alive keyword, as demonstrated in the example.
;;; web-sockets.el --- communicate via web-sockets
(lexical-let* ((web-socket-port 9009)
(web-socket-page
(format "<html>
<head>
<script type=\"text/javascript\">
var ws;
function connect(){
ws = new WebSocket(\"ws://localhost:%d/\");
ws.onopen = function() { alert(\"connected\"); };
ws.onmessage = function(msg) { alert(\"server: \" + msg.data); };
ws.onclose = function() { alert(\"connection closed\"); };
}
function message(){ ws.send(\"foo\"); }
function close(){ ws.close(); };
</script>
</head>
<body>
<ol>
<li>Press \"connect\" to initialize the web socket connection to
the server. The server will complete the web socket
handshake at which point you'll see an alert with the text
\"connected\".</li>
<li>Press \"message\" to send the string \"foo\" to the server.
The server will reply with the text \"you said: foo\" which
you will see in an alert as \"server: you said: foo\".</li>
<li>Press \"close\" to close the connection. After the server
responds with a close frame you will see an alert with the
text \"connection closed\".</li>
</ol>
<a href=\"javascript:connect()\">connect</a>
<a href=\"javascript:message()\">message</a>
<a href=\"javascript:close()\">close</a>
</body>
</html>" web-socket-port)))
(ws-start
(lambda (request)
(with-slots (process headers) request
;; if a web-socket request, then connect and keep open
(if (ws-web-socket-connect request
(lambda (proc string)
(process-send-string proc
(ws-web-socket-frame (concat "you said: " string)))))
(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))))
web-socket-port))
Next: Chunked Transfer Encoding, Previous: File Upload, Up: Usage Examples [Contents][Index]