June 17, 2010

WebSocket Handshake 76 in Erlang

Websocket get update and handshacke get broken… this is not for sure and end version of the protocol, but since newer versions of chrome are using it and its not compatible with previous version its a good option to implement both.

In order to understand what changed, i recommend you to read the great resume done in Axod’s Hack .

Mainly you need to get 3 keys, 2 of them are send in the headers and the 3rd is sent after the headers. Then you need to do some “weird” transformation with the 3 and send them back after the header.

After having the 3 keys, here is the code in erlang to build the challenge to send back to client:

Now that you have the challenge just build the response like this :


Challenge = build_challenge(Key1,Key2,Key3),

HandshakeServer = [
        "HTTP/1.1 101 Web Socket Protocol Handshake\r\n",
	"Upgrade: WebSocket\r\n",
	"Connection: Upgrade\r\n",
	"Sec-WebSocket-Origin: ", Origin , "\r\n",
	"Sec-WebSocket-Location: ws://", Location , "\r\n\r\n",
	Challenge
],

As soon i can i will create a new branch on misultin web server so you can see the code working.

  • Zoltán Lajos Kis

    Hi Roberto,

    Note, that the reason phrase is now "WebSocket Protocol Handshake" (no space between Web and Socket).

    Also you could replace get_key_number with a simple list comprehension:
    IKey1 = [D || D <- Key, $0 =< D, D =< $9]

  • thank you sergio. if this code is in MIT let me know, i'll probably include part of it in the forecoming new release of misultin.

  • darkua

    Sorry for the late answer...

    Sure its MIT, fell free to use it. i had the intention to create a brunch of it on misultin, but to busy lately... by the way thanks for you work, misultin looks really good :)

blog comments powered by Disqus