Node.js Websockets Socket.IO

I cannot fire events on the client side, see code / explanation:

Ok, so I got this job (think)

Client 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 a page in Chrome, I see the logs written in the shell.

Here is 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 problem now is to run any of these javascript socket warnings.

In addition, this error appears 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 the "bad title"?

Thanks Dan

+5
source share
6 answers

HTML- nginx, - - - , nginx HTTP HTTP -. , websockets - go, Flash crossdomain-, :

<policy-file-request/>\x00

nginx HTTP 400.

, node.js, :

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

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

    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>
+3

- nginx. , , nginx Apache - . , websockets.

nginx -?

+2

, , socket.io. npm , github , . , .

+2

- , 80, 26970.

0

" http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75, Chrome 4 5, :

HTTP/1.1 101 Socket

: WebSocket

:

There was a quick google search, and here is what I came up with.

0
source

It’s pretty accurate that the method name is “addEvent” and not “on”, although “on” will definitely be more than “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');
});
-3
source

All Articles