Note that there are some explanatory texts on larger screens.

plurals
  1. POwebsocket handshake problem
    text
    copied!<p>I'm using python to implement a simple websocket server. The handshake I'm using comes from <a href="http://en.wikipedia.org/w/index.php?title=WebSockets&amp;oldid=372387414" rel="noreferrer">http://en.wikipedia.org/w/index.php?title=WebSockets&amp;oldid=372387414</a>.</p> <p>The handshake itself seems to work, but when I hit send, I get a javascript error:</p> <blockquote> <p>Uncaught Error: INVALID_STATE_ERR: DOM Exception 11</p> </blockquote> <p>Here's the html:</p> <pre><code>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;ws_json&lt;/title&gt; &lt;/head&gt; &lt;body onload="handleLoad();" onunload="handleUnload();"&gt; &lt;input type="text" id='input' /&gt; &lt;input type="button" value="submit" onclick="handleSubmit()" /&gt; &lt;div id="display"&gt;&lt;/div&gt; &lt;script type="text/javascript"&gt; function showmsg(str){ display = document.getElementById("display"); display.innerHTML += "&lt;p&gt;" + str + "&lt;/p&gt;"; } function send(str){ ws.send(str.length); ws.send(str); } function handleSubmit(){ input = document.getElementById('input'); send(input.value); input.focus(); input.value = ''; } function handleLoad(){ ws = new WebSocket("ws://localhost:8888/"); ws.onopen = function(){ showmsg("websocket opened."); } ws.onclose = function(){ showmsg("websocket closed."); } } function handleUnload(){ ws.close(); } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And here's the python code:</p> <pre><code>import socket import threading import json PORT = 8888 LOCATION = "localhost:8888" def handler(s): print " in handler " ip, _ = s.getpeername() print "New connection from %s" % ip request = s.recv(1024) print "\n%s\n" % request print s.getpeername() # send response response = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" response += "Upgrade: WebSocket\r\n" response += "Connection: Upgrade\r\n" try: peername = s.getpeername() response += "Sec-WebSocket-Origin: http://%s\r\n" % peername[0] # % request[request.index("Origin: ")+8:-4] except ValueError: print "Bad Request" raise socket.error response += "Sec-WebSocket-Location: ws://%s\r\n" % LOCATION response += "Sec-WebSocket-Protocol: sample" response = response.strip() + "\r\n\r\n" print response s.send(response) while True: length = s.recv(1) print length if not length: break length = int(length) print "Length: %i" % length data = s.recv(length) print "Received: %s" % data print "" s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind(('localhost', PORT)) s.listen(5) print "server is running..." while True: sock, addr = s.accept() threading.Thread(target=handler, args=(sock, )).start() </code></pre> <p>Does anyone know what I'm doing wrong here?</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