views:

825

answers:

4

I can't get the client-side events to fire, please see the code/explanation:

Okay, so I got this working(I think)

Client-side code:

<script src="./Socket.IO/socket.io.js"></script>
<script>
    io.setPath('./Socket.IO/');

    var socket = new io.Socket('jayz.danstanhope.webfactional.com', { 'port': 80 });

    socket.on('connect', function () {
        alert('connect');
    });
    socket.on('message', function (msg) {
        alert('message' + msg);
    });
    socket.on('close', function () {
        alert('close');
    });
    socket.on('disconnect', function () {
        alert('disconnect');
    });
    socket.connect();

</script>

Server-side code:

var sys = require("sys")
  , fs = require("fs")
  , path = require("path")
  , http = require("http");
var io = require('/home/danstanhope/webapps/htdocs/Socket.IO-node');

var server = http.createServer(function (req, res) {
    //your normal server code
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.write('Hello world');
    res.end();
});

server.listen(26970);
server = io.listen(server);
server.on('connection', function(client){
    sys.log('client connected');
});

When I refresh the page in Chrome I can see logs being written in Shell.

Here's what I see:

danstanhope@web146 htdocs]$ node server.js
9 Aug 19:19:37 - socket.io ready - accepting connections
9 Aug 19:19:40 - Initializing client with transport "websocket"
9 Aug 19:19:40 - Client 21789167495444417 connected
9 Aug 19:19:40 - client connected
9 Aug 19:19:40 - Client 21789167495444417 disconnected

The only issue now is getting any of those javascript socket alerts to fire.

Also, this error is showing up in Chrome:

Bad Upgrade header: Server: nginx

Date: Wed, 11 Aug 2010 23:06:06 GMT

Transfer-Encoding: chunked

Connection: keep-alive

Upgrade: WebSocket

Any ideas on how to fix a "bad header"?

Thanks, Dan

A: 

Having no idea about websockets at all, but in the client code you initialize the socket with port 80 but you tell the server to listen at port 26970.

Kitto
Hey, that's because I'm forwarding the port to 26970. So I think that'll work fine.Thanks anyway though :)
Dan
A: 

Pretty sure the method name is "addEvent", not "on", though "on" would definitely be more "node-y" :)

socket.addEvent('connect', function () {
    alert('connect');
});
socket.addEvent('message', function (msg) {
    alert('message' + msg);
});
socket.addEvent('close', function () {
    alert('close');
});
socket.addEvent('disconnect', function () {
    alert('disconnect');
});
tedsuo
Thanks a lot for the reply,That didn't fix it though :(Same issue, events not firing.
Dan
A: 

"As of http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75, which is used in Chrome 4 and 5, response header must start with :

HTTP/1.1 101 Web Socket Protocol Handshake

Upgrade: WebSocket

Connection: Upgrade"

Did a quick google search and this is what i came up with.