使用反向代理时,socket.io示例有时无法连接客户端 [英] socket.io example sometimes not connecting client side when using a reverse proxy

查看:332
本文介绍了使用反向代理时,socket.io示例有时无法连接客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用node-http-proxy,我为路由请求设置了反向代理:

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

现在,当我在 http://socket.io/#how-使用,套接字有时不与客户端连接.我创建了两个文件对此进行测试:server.js和index.html.要启动节点应用程序,我运行server.js.

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>

当客户端不连接时,套接字与服务器连接后,我会以+/- 10秒的间隔反复获得以下输出:

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

现在,当我几次刷新浏览器时,套接字始终与服务器连接(即,它始终记录套接字已连接" ),但有时它不连接客户端: console.log(socket.socket.connected)有时在刷新index.html之后重复打印"false" ,并且在另一页刷新之后,它可能会重复打印"true" "false" <如果套接字没有或没有与客户端连接,请再次输入/em>.

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.

当我不使用反向代理时,该示例在客户端工作,因此当我在www.example.com的端口80上运行server.js时.如果有人可以指出我的原因,那就太好了.我正在使用node.js v0.8.23,socket.io版本0.9.14和node-http-proxy版本0.10.1.

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.

更新

可能我实际上正在使用节点v0.10.21.我以为我通过使用 nvm 切换节点版本来使用v0.8.23,但是由于某些原因,它一直保持切换回v0.10. 已知问题是http-proxy不支持Web套接字的对于高于0.8的节点版本,则可能是原因.在找到更好的方法之前,我一直在使用robertklep的解决方案.

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.

推荐答案

这确实是我使用的节点版本. Node-http-proxy不适用于节点版本> 0.8(请参见 Node http代理使用proxytable和websockets ).我以为我正在使用v0.8.23,但实际上我正在使用v0.10.21.通过使用 n ,我可以确定节点可以在v0.8.23上正常工作,现在看来可以正常工作了.我强烈建议使用n重置节点版本.

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.

这篇关于使用反向代理时,socket.io示例有时无法连接客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆