Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I tested your code on Firefox 4 and got the same error upon hitting send, however before that I got </p> <blockquote> <p>Firefox can't establish a connection to the server at ws://localhost:8888/.</p> </blockquote> <p>which is probably why the WebSocket object was destroyed. I suspect your handshake response is missing something, so Firefox is closing the socket.</p> <p>From the Wikipedia article on Websockets:</p> <blockquote> <p>The Sec-WebSocket-Key1 and Sec-WebSocket-Key2 fields and the eight bytes after the fields are random tokens which the server uses to construct a 16 byte token at the end of its handshake to prove that it has read the client's handshake.</p> </blockquote> <p>Your server's response does not have this special number at the bottom, So I think we need to figure out how to generate it, and include it.</p> <p><strong>EDIT: How to generate that number</strong></p> <p>Lets start with key1, key2, and the 8 bytes at the end of the handshake</p> <pre><code>key1 = "18x 6]8vM;54 *(5: { U1]8 z [ 8" key2 = "1_ tx7X d &lt; nw 334J702) 7]o}` 0" end8 = "Tm[K T2u" </code></pre> <p>We make a number for each key by ignoring every character that is not a digit 0-9. In Python:</p> <pre><code>def numFromKey(key): return int(filter(lambda c: c in map(str,range(10)),key)) </code></pre> <p>next we divide that number by the number of spaces in the original key string, so here is a is a function that counts the spaces in a string.</p> <pre><code>def spacesIn(key): return len(filter(lambda c: c==' ',key)) </code></pre> <p>The two numbers resulting from the keys are:</p> <pre><code>pkey1 = numFromKey(key1)/spacesIn(key1) pkey2 = numFromKey(key2)/spacesIn(key2) </code></pre> <p>Now we need to concatenate the bytes of pkey1, pkey2, and end8. The processed keys need to be represented as 32 bit Big-Endian numbers. </p> <pre><code>from struct import pack catstring = pack('&gt;L',pkey1) + pack('&gt;L',pkey2) + end8 </code></pre> <p>Then we take the md5 hash of those bytes to get the magic number that we tack on the end of the handshake</p> <pre><code>import md5 magic = md5.new(catstring).digest() </code></pre> <p>Thats how I think it works at least</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