0

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?

pimvdb
  • 151,816
  • 78
  • 307
  • 352
jack.cap.rooney
  • 1,306
  • 3
  • 21
  • 37
  • You can very easily test it with JSFiddle. Here's the code snippet from websocket.org that works just fine: http://jsfiddle.net/6Wx2f/ – Peter Moskovits Aug 07 '12 at 05:47

2 Answers2

2

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.

Community
  • 1
  • 1
pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • how can i configure my local apache to use websocket protocol? – jack.cap.rooney Sep 27 '11 at 19:00
  • @jack.cap.rooney: You don't need Apache. In fact a WebSockets server does not use Apache at all since it's not a HTTP server. You need something standalone. I prefer Node.js because you can program the server in JavaScript, and there are several libraries available. – pimvdb Sep 27 '11 at 20:25
  • Actually, websocket.org is pretty reliable, and it has been supporting the final protocol version for a very long time. – Peter Moskovits Aug 07 '12 at 05:48
  • @Peter Moskovits: Thanks for the heads up; as you can see I wrote this almost a year ago. – pimvdb Aug 07 '12 at 09:26
1

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.

Leigh
  • 28,765
  • 10
  • 55
  • 103
Achex
  • 11
  • 1