1 ;;; web-sockets.el --- communicate via web-sockets
2 ;; Copyright (C) 2014 Free Software Foundation, Inc.
4 (lexical-let* ((web-socket-port 9009)
8 <script type=\"text/javascript\">
11 ws = new WebSocket(\"ws://localhost:%d/\");
13 ws.onopen = function() { alert(\"connected\"); };
14 ws.onmessage = function(msg) { alert(\"server: \" + msg.data); };
15 ws.onclose = function() { alert(\"connection closed\"); };
18 function message(){ ws.send(\"foo\"); }
20 function close(){ ws.close(); };
26 <li>Press \"connect\" to initialize the web socket connection to
27 the server. The server will complete the web socket
28 handshake at which point you'll see an alert with the text
31 <li>Press \"message\" to send the string \"foo\" to the server.
32 The server will reply with the text \"you said: foo\" which
33 you will see in an alert as \"server: you said: foo\".</li>
35 <li>Press \"close\" to close the connection. After the server
36 responds with a close frame you will see an alert with the
37 text \"connection closed\".</li>
40 <a href=\"javascript:connect()\">connect</a>
41 <a href=\"javascript:message()\">message</a>
42 <a href=\"javascript:close()\">close</a>
44 </html>" web-socket-port)))
47 (with-slots (process headers) request
48 ;; if a web-socket request, then connect and keep open
49 (if (ws-web-socket-connect request
51 (process-send-string proc
52 (ws-web-socket-frame (concat "you said: " string)))))
53 (prog1 :keep-alive (setq my-connection process))
54 ;; otherwise send the index page
55 (ws-response-header process 200 '("Content-type" . "text/html"))
56 (process-send-string process web-socket-page))))