NodeJS、http 代理、socket.io

发布于 2024-12-24 21:59:19 字数 1374 浏览 0 评论 0原文

我正在端口 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

物价感观 2024-12-31 21:59:19

据我所知,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.

凉城凉梦凉人心 2024-12-31 21:59:19

难道你不应该在地址127.0.0.1:3030上请求socket.io吗?并连接到 mydomain.com:3030。

如果我理解正确的话,您在 3030 端口上运行 Express 应用程序,因此您需要在客户端 JS 中连接到那里。

$(function() {
    console.log('connecting to chat...')
    var socket = io.connect('http://mydomain.com:3030');

    socket.on('connected',function(){
    console.log('connected')
    });
});

我猜...

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.

$(function() {
    console.log('connecting to chat...')
    var socket = io.connect('http://mydomain.com:3030');

    socket.on('connected',function(){
    console.log('connected')
    });
});

I guess...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文