socket.io example sometimes not connecting client side when using a reverse proxy

守給你的承諾、 提交于 2019-12-08 09:35:16

问题


Using node-http-proxy, I've set up a reverse proxy for routing requests:

var httpProxy = require('http-proxy');
var server = httpProxy.createServer({
    hostnameOnly: true,
    router: {
        'www.example.com': 'localhost:3002'
    }
}).listen(80);

Now, when I run the first example on http://socket.io/#how-to-use, the socket is sometimes not connecting with the client. I created two files to test this: server.js and index.html. To start the node application, i run server.js.

server.js:

var app = require('http').createServer(handler)
    , io = require('socket.io').listen(app)
    , fs = require('fs')

app.listen(3002);

function handler (req, res) {
    fs.readFile(__dirname + '/index.html',
    function (err, data) {
        if (err) {
            res.writeHead(500);
            return res.end('Error loading index.html');
        }

        res.writeHead(200);
        res.end(data);
    });
}

io.sockets.on('connection', function (socket) {
    console.log("Socket connected");
});

index.html:

<script src="/socket.io/socket.io.js"></script>
<script>
    var socket = io.connect();
    setInterval(function() {
        console.log(socket.socket.connected);
    }, 1000)
</script>

When the client does not connect, after the socket connects with the server, I repeatedly get the following output with intervals of +/- 10 seconds:

debug - setting request GET /socket.io/1/xhr-polling/Cqcw5xUjQ-B-Hw3FGF7Y?t=1385128607702
debug - setting poll timeout
debug - discarding transport
debug - cleared heartbeat interval for client Cqcw5xUjQ-B-Hw3FGF7Y

Now, when I refresh the browser a few times, the socket always connects with the server (that is, it always logs "Socket connected"), but sometimes it does not connect client side: console.log(socket.socket.connected) sometimes repeatedly prints "false" after refreshing index.html, and after another page refresh, it may repeatedly print "true" or "false" again if the socket did not or did connect with the client.

The example does work client-side when I do not use the reverse proxy, so when I run server.js on port 80 on www.example.com. It would be great if someone could point me out what could be the cause of this problem. I am using node.js v0.8.23, socket.io version 0.9.14 and node-http-proxy version 0.10.1.

UPDATE

Probably, I am actually using node v0.10.21. I thought I was using v0.8.23 by switching the node version using nvm, but for some reason it keeps switching back to v0.10. It is a known issue that http-proxy does not support web sockets for node versions later than 0.8, so that may be the cause. I am using robertklep's solution until I find something better.


回答1:


I think the issue might be that socket.io is initially trying to use WebSockets as a transport medium and when that doesn't work (I don't know if node-http-proxy can proxy WS connections), it falls back to a transport that does work (xhr-polling) but the client and/or server gets confused in the process.

Try disabling the websocket and flashsocket transports to see if that makes it more reliable:

io.set('transports', [ 'xhr-polling', 'jsonp-polling', 'htmlfile' ]);

(more info)




回答2:


It really was the node version I was using. Node-http-proxy does not work for node version >0.8 (see Node http proxy with proxytable and websockets). I thought I was using v0.8.23 but I was actually using v0.10.21. By using n, I could get node working for v0.8.23 for sure, and now it seems to work. I highly recommend n for resetting the node version.



来源:https://stackoverflow.com/questions/20146720/socket-io-example-sometimes-not-connecting-client-side-when-using-a-reverse-prox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!