Note that there are some explanatory texts on larger screens.

plurals
  1. POKeeping the WebSocket connection alive
    text
    copied!<p>I'm doing a study on WebSocket protocol and trying to implement a simple ECHO service for now with Python on the backend. It seems to work fine but the connection drops right after being established.</p> <p>Here is my client:</p> <pre><code>&lt;!doctype html&gt; &lt;head&gt; &lt;script type="text/javascript" src="jquery.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; function Client() { //var ws = new WebSocket("ws://echo.websocket.org"); // this works fine var ws = new WebSocket("ws://localhost:8000"); ws.onopen = function(e){ $("#response").append("&gt;&gt; Connected&lt;br /&gt;"); } ws.onclose = function(e){ $("#response").append("&gt;&gt; Disconnected&lt;br /&gt;"); } ws.onerror = function(e){ $("#response").append("&gt;&gt; ERROR: " + e.data + "&lt;br /&gt;"); } ws.onmessage = function(e){ $("#response").append("&gt; " + e.data + "&lt;br /&gt;"); } this.sendCmd = function() { var message = $("#cmd").val(); $("#response").append(message + "&lt;br /&gt;"); ws.send(message); return false; } this.disconnect = function() { ws.close(); } } // onload $(function() { $("#response").append("&gt;&gt; Connecting&lt;br /&gt;"); client = new Client(); $("#send").click(client.sendCmd); $("#disconnect").click(client.disconnect); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;input type="text" name="cmd" id="cmd" /&gt; | &lt;a href="#" id="send"&gt;Send&lt;/a&gt; | &lt;a href="#" id="disconnect"&gt;Disconnect&lt;/a&gt;&lt;br /&gt; &lt;hr /&gt; &lt;span id="response"&gt;&lt;/span&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Here is the server:</p> <pre><code>import SocketServer import socket from hashlib import sha1 from base64 import b64encode PORT = 8000 MAGIC = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" class Handler(SocketServer.BaseRequestHandler): # incoming connection def setup(self): self.data = self.request.recv(1024).strip() print "connection established", self.client_address self.headers = self.headsToDict(self.data.split("\n")) # incoming message def handle(self): # its a handshake if "Upgrade" in self.headers and self.headers["Upgrade"] == "websocket": key = self.headers["Sec-WebSocket-Key"] accept = b64encode(sha1(key + MAGIC).hexdigest().decode('hex')) response = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" # "HTTP/1.1 101 Switching Protocols\r\n" print "&lt; HTTP/1.1 101 Web Socket Protocol Handshake" # "HTTP/1.1 101 Switching Protocols\r\n" response += "Upgrade: websocket\r\n" print "&lt; Upgrade: websocket" response += "Connection: Upgrade\r\n" print "&lt; Connection: Upgrade" response += "Sec-WebSocket-Accept: "+accept+"\r\n\r\n" print "&lt; Sec-WebSocket-Accept: "+accept self.request.send(response) # its a normal message, echo it back else: print self.data self.request.send(self.data) # connection dropped def finish(self): print "connection lost", self.client_address # convert a list of headers to a dictionary for convenience def headsToDict(self, hdata): rzygi = {} for item in hdata: print '&gt;', item item = item.split(':') if len(item) &gt; 1: rzygi[item[0].strip()] = item[1].strip() return rzygi server = SocketServer.TCPServer(("", PORT), Handler) server.socket_type = socket.SOCK_STREAM # didnt help print "serving at port", PORT try: server.serve_forever() except KeyboardInterrupt: pass server.server_close() </code></pre> <p>As mentioned, the connection is established successfully but then drops straight away, which makes me think the code is correct but there is something missing to keep the socket open. Here is the server output:</p> <pre><code>serving at port 8000 connection established ('127.0.0.1', 52633) &gt; GET / HTTP/1.1 &gt; Upgrade: websocket &gt; Connection: Upgrade &gt; Host: localhost:8000 &gt; Sec-WebSocket-Origin: http://localhost &gt; Sec-WebSocket-Key: qWGnhdFQ6l8Xs9awgQURfA== &gt; Sec-WebSocket-Version: 8 &lt; HTTP/1.1 101 Web Socket Protocol Handshake &lt; Upgrade: websocket &lt; Connection: Upgrade &lt; Sec-WebSocket-Accept: fei4E4LQvPnf4y2ilebVsxRofvc= connection lost ('127.0.0.1', 52633) </code></pre> <p>How do I keep the socket open?</p> <hr> <p>edit: server code comments</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload