Reputation: 1306
I'm trying this simple websocket example on Google Chrome:
var wsUri = "ws://echo.websocket.org/";
var output;
function init() {
output = document.getElementById("output");
testWebSocket();
}
function testWebSocket() {
websocket = new WebSocket(wsUri);
websocket.onopen = function(evt) {
onOpen(evt)
};
..............
..............
function onOpen(evt) {
writeToScreen("CONNECTED");
doSend("WebSocket rocks");
}
function onClose(evt) {
writeToScreen("DISCONNECTED");
}
window.addEventListener("load", init, false);
But i always receive only DISCONNECT!
There is something wrong?
Do I have to enable WebSockets protocol in my local Apache? If yes how to?
Upvotes: 0
Views: 1526
Reputation: 11
You need to specify that websocket is a variable. Change this line:
websocket = new WebSocket(wsUri);
to this:
var websocket = new WebSocket(wsUri);
Hope it helps. This solved some problems for me.
Upvotes: 1
Reputation: 154838
This server is not reliable. It even fails on their own demo page for Chrome 14.
The response for a WebSockets request of Chrome 14 is this, which is obviously not correct:
HTTP/1.1 200 OK
Server: Kaazing Gateway
Date: Tue, 27 Sep 2011 14:07:53 GMT
Content-Length: 0
Note that Chrome just switched to a new draft of the WebSockets protocol, which is a complete overhaul. This means that the server has to return a different handshake response and also has to decode messages that are sent, which was not the case with the previous draft. It might just be that they did not upgrade their server yet.
What you probably want is setting up your own server which is compliant with the new draft and test it on that server.
There are a lot of libraries for WebSockets servers popping up everywhere; you can have a look here and pick the server language of your choice.
Upvotes: 2