How do I know which version of websocket a client is using?

I wrote (not so) a simple websocket server in python that listens on a port, does a handshake, and then sends the client a sequence of messages (at random intervals) containing numbers.

I wrote a javascript client and tested it under Chrome and Safari. I found that Chrome and Safari use different versions of WebSocket. For example, Chrome uses Sec-WebSocket-Key (and expects Sec-WebSocket-Accept), while Safari sends Sec-WebSocket-Key1, Sec-WebSocket-Key2, and an 8 byte bunch after the header instead.

I have implemented a handshake function on the server that detects the required handshake and performs it. This issue has been resolved. Web Layout is correctly opened from Chrome or Safari (OSX, Windows and IOS5 versions).

But I have a different problem. Apparently Safari sends and waits for messages limited to 0x00 and 0xFF, while Chrome sends and waits for framed and masked data (using the newer version of the websocket specification).

I want to have one server that adapts to the client's expectations. My question is, how can I tell in advance if the data should be sent to the frame or if 0x00,0xFF is limited?

I guess I could assume that if the handshake protocol is based on Key1 and Key2 the client is Safari and then uses 0x00 0xFF to delimit the data, whereas if it uses Sec-WebSocket-Key the client is Chrome and then use the framed data. However, I am not satisfied with this solution as it is not generic. Ideas?

+3


source to share


3 answers


There are published standards for the hixie-76 protocol used by Safari (desktop and mobile) and RFC 6455 used by Chrome and others.



You can use the type of handshake that is required to determine the protocol version the client is talking about, what will exist when reading messages, and which frame should be used when writing messages.

+3


source


To answer your original question, since I had the same need, I found that window.WebSocket.CLOSED is 3 in the latest implementation, when on an older version it is 2 (Android native browser for example).

So, now I'll run a test to find support for the latest WebSocket protocol:



if('WebSocket' in window
    &&'function' === typeof window.WebSocket
    &&3 === window.WebSocket.CLOSED) {
// hurra !
}

      

I've tested it in Chrome, Firefox (where it succeeded) and Android browser (where it doesn't work). Wish this helps some people.

+1


source


Simple: This is only one version of WebSockets. The rest are drafts. Now that there is no RFC, you are not officially encouraged to implement anything. Recent versions of Firefox and Chrome have implemented the RFC version and are automatically updated. Drawings will stop being used by clients very quickly, so there is no point in trying to implement them, even to improve the interaction a little.

They are no longer relevant, and add a dash. He was not going to promote any of the browser vendors to keep projects going. Indeed, Mozilla waited until the specification before decrypting it. Likewise, browser developers have realized that any applications written for draft should be demo code, not production code, and there is no expectation that they will support server projects more than they support a client code project.

So stick with one specification and forget about early draft implementations.

-1


source







All Articles