I've been experimenting with WebSockets for the past couple of days and I'm having some mixed experiences with the new, very cool, technology. I've written a simple chat client that uses the latest release from HTML5 Labs, which I believe is the hybi-09 draft spec release. The client works great in Chrome (dev channel v14.0). Everything functions as it should. However, in every other major browser that natively supports WebSockets (FireFox (v6.0b) (Yes, I did turn on WebSockets functionality), Safari (v5.1)), it can't connect for some reason. Here's some of my client code:
$(document).ready(connect);
function connect() {
if ('WebSocket' in window) {
websocket = new WebSocket('ws://' + window.location.hostname + ':4502/chat');
}
else if ('MozWebSocket' in window) {
websocket = new MozWebSocket('ws://' + window.location.hostname + ':4502/chat');
}
else {
//not supported
return;
}
websocket.onopen = function () {
//do some setup stuff
};
websocket.onclose = function () {
//DOH
};
websocket.onmessage = function (e) {
//Do some stuff with e.data
};
}
and some (C#) server code:
static void Main(string[] args)
{
var host = new WebSocketsHost<ReverseService>();
host.AddWebSocketsEndpoint("ws://" + Environment.MachineName + ":4502/chat");
host.Open();
Console.ReadLine();
}
Like I said, it connects fine in Chrome and hits the .onopen function as it should. In FF and Safari, it goes straight to the onclose function and never connects. In FF, I get the following errors:
"NetworkError: 501 Not Implemented - http://localhost:4502/chat"
Firefox can't establish a connection to the server at ws://localhost:4502/chat
And in Safari:
WebSocket frame (at 4294967295 bytes) is too long.
The only thing I can think of is some kind of backwards compatibility issue. I believe Chrome 14.x implements the draft 10 spec of hybi WebSockets and I think FF 6 implements draft 07 or 08 and I'm not sure about Safari 5.1. If anyone has any insight as to what the problem is and/or how/if I can fix it, I'd appreciate the help. Thanks!
Chrome 14 and Firefox 7 (Aurora build, prefixed with "Moz" but enabled by default) support the HyBi-10 version of the protocol. Everything else that has native WebSockets support is still using the Hixie-76 version of the protocol.
There are server implementations that already support the HyBi protocol and many more will soon now that Chrome 14 has it natively. There are some that have support for both Hixie-76 and the newer HyBi versions of the protocol (libwebsockets, websockify). I'm not particularly surprised that Microsoft's prototype server implementation only supports one version of the protocol (since they were not in the game during the Hixie period).
Update:
Some server options:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With