Your handshake block is incomplete. There should be an empty string followed by 8 bytes of additional data (or key3).
The server needs to create a reverse handshake that looks something like this:
HTTP/1.1 101 Web Socket Protocol Handshake\r Upgrade: WebSocket\r Connection: Upgrade\r Sec-WebSocket-Origin: http://localhost:8080\r Sec-WebSocket-Location: ws://localhost:9999/\r Sec-WebSocket-Protocol: sample\r \r [16 byte md5 hash]
The 16-byte hash returned by the server is calculated as follows:
take numbers from key1 data and convert them to a number. Divide this number by the number of data spaces key1. This gives you a 4 byte key1.
do the same with the key2 data to get the key2 number.
create an array of 16 bytes, packs of key1 (4 bytes), followed by key2 (4 bytes), followed by 8 bytes of additional data received from the client (8 bytes).
md5 sum a 16 byte array to get a new 16 byte array. This is what the client wrote to complete the handshake response.
Until the server returns the handshake, the client will display “CONNECTION”.
The response to connecting to the server is described in more detail in section 5.2 of WebSockets version 76
Update on the second question (in the answer section):
If all you are trying to do is send data from the extension, then you can simply use the XMLHttpRequest POST request (AJAX). Since you are getting something from the request, I suspect that latency is not so important for your application. XMLHttpRequest should easily support extensions for both Firefox and Chrome.
On the other hand, if latency is important or if your server where the data is sent is exclusively a socket server, adding WebSockets support will not be much more complicated than adding HTTP support (in fact, it might be easier). WebSockets support should also support extensions in both Chrome and firefox (4.0 onwards).
source share