开发者

Node.js Websockets Socket.IO

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('clie开发者_如何学编程nt 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


You can proxy the standard HTML requests with nginx but not the websockets nor the flashsockets connection due to the fact that nginx cannot forward non HTTP 1.0 traffic through a proxy yet. So websockets are a no go and Flash won't be able to send it's crossdomain policy as it looks a bit like this:

<policy-file-request/>\x00

Which nginx happily throws up an HTTP 400 error on.

In order to make it work, you must connect the client to port your node.js app is listening on:

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


As of right now, it isn't possible to leverage websockets from an nginx machine. This may have been corrected recently, but all my research says that nginx and Apache can't host websockets at the moment. It seems like most reverse proxies disagree with websockets.

Can nginx be used as a reverse proxy for a backend websocket server?


I had this exact same issue a while back and the problem ended up being caused by two different versions of socket.io. I used npm to import one version then got the client side version on github from a different developer and they were incompatible. Double check you aren't inadvertently using someone else's fork.


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.


"As of https://datatracker.ietf.org/doc/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.


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');
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜