NodeJS、http 代理、socket.io
我正在端口 3030 上运行 Express Web 应用程序,但它是通过 http-proxy 代理的。该网站以这种方式工作正常,但 socket.io 将无法连接。当我的控制台显示时,它确实收到了一些内容:
debug - served static content /socket.io.js
debug - client authorized
info - handshake authorized 11319742841450363586
debug - setting request GET /socket.io/1/websocket/11319742841450363586
debug - set heartbeat interval for client 11319742841450363586
debug - client authorized for
debug - websocket writing 1::
app.js
app.listen(3030)
io = require('socket.io')
socket = io.listen(app)
socket.on('connection', function(client) {
console.log('new connection')
})
chat.js
$(function() {
console.log('connecting to chat...')
var socket = io.connect('http://mydomain.com:80')
socket.on('connected',function(){
console.log('connected')
})
})
但是,console.log 语句都不会显示在客户端或服务器端。我做错了什么?
编辑 - 添加了 http 代理代码
var httpProxy = require('http-proxy')
, proxyTable = {
router: {
'lou.mydomain.com': '127.0.0.1:3030'
, 'foe.mydomain.com': '127.0.0.1:3000'
// and some others
}
}
, proxyServer = httpProxy.createServer(proxyTable);
proxyServer.listen(80);
I am running a express web application on port 3030, but it is proxied through http-proxy. The site works fine this way, but socket.io will not connect. It does recieve something as my console displays:
debug - served static content /socket.io.js
debug - client authorized
info - handshake authorized 11319742841450363586
debug - setting request GET /socket.io/1/websocket/11319742841450363586
debug - set heartbeat interval for client 11319742841450363586
debug - client authorized for
debug - websocket writing 1::
app.js
app.listen(3030)
io = require('socket.io')
socket = io.listen(app)
socket.on('connection', function(client) {
console.log('new connection')
})
chat.js
$(function() {
console.log('connecting to chat...')
var socket = io.connect('http://mydomain.com:80')
socket.on('connected',function(){
console.log('connected')
})
})
However, neither of the console.log statements ever display on the client or server side. What am I doing wrong?
EDIT - Added http-proxy code
var httpProxy = require('http-proxy')
, proxyTable = {
router: {
'lou.mydomain.com': '127.0.0.1:3030'
, 'foe.mydomain.com': '127.0.0.1:3000'
// and some others
}
}
, proxyServer = httpProxy.createServer(proxyTable);
proxyServer.listen(80);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,node-http-proxy 不适用于 Node > 的 WebSockets。 0.6.x 因为一个错误(这是几周前的事)。他们说他们正在修复这个问题,所以他们可能还没有修复这个问题。如果您无法使其正常工作,请尝试 bouncy。
As far as I knew, node-http-proxy doesn't work with WebSockets with Node > 0.6.x because of a bug (this was a couple of weeks ago). They said they were working on a fix, so it's possible that they didn't fix this yet. If you can't get that to work, try bouncy.
难道你不应该在地址127.0.0.1:3030上请求socket.io吗?并连接到 mydomain.com:3030。
如果我理解正确的话,您在 3030 端口上运行 Express 应用程序,因此您需要在客户端 JS 中连接到那里。
我猜...
Shouldn't you ask for socket.io on address 127.0.0.1:3030? And connecting to mydomain.com:3030, also.
If I understand right, you run express application on 3030 port, so that you need to connect there to in client-side JS.
I guess...