30

我有一个正在运行的代理,它只访问我的 node.js 服务器的路径/mysubdir 我如何为这种情况配置 socket.io?

在我的客户端代码中,我尝试过:

var socket = io.connect('http://www.example.com/mysubdir');

但后来我注意到底层的socket.io(或engine.io)http请求正在命中

http://www.example.com/socket.io/?EIO=3&transport=polling&t=1410972713498-72`

我要他们打

http://www.example.com/mysubdir/socket.io.....

我必须在客户端和服务器上配置什么吗?

4

4 回答 4

39

在我的服务器中,我必须

var io = require('socket.io')(httpServer, {path: '/mysubdir/socket.io'})`

在我的客户中,我不得不

<script src="http://www.example.com/mysubdir/socket.io/socket.io.js"></script>

并且

var socket = io.connect('http://www.example.com', {path: "/mysubdir/socket.io"});`
于 2014-09-17T17:33:59.590 回答
11

就我而言,我使用 nginx 作为反向代理。轮询时出现 404 错误。这就是我的解决方案。

节点服务器的 url 是https://example.com/subdir/

在 app.js 中,我实例化了 io 服务器

var io = require('socket.io')(http, {path: '/subdir/socket.io'});

在我使用的 html 中

socket = io.connect('https://example.com/subdir/', {
    path: "/subdir"
});

干杯,
卢克。

于 2015-11-04T00:39:21.133 回答
7

使用 nginx,这是一个无需更改 socket.io 服务器应用程序中的任何内容的解决方案:

在 nginx 配置文件中:

location /mysubdir {
   rewrite ^/mysubdir/(.*) /socket.io/$1 break;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";
   proxy_http_version 1.1;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header Host $host;
   proxy_pass http://127.0.1.1:3000;
}

在服务器中:

var io = require('socket.io')(3000)

在客户端:

var socket = io.connect('https://example.com/', {
    path: "/mysubdir"
})
于 2018-10-06T14:00:13.920 回答
3

@Drew-LeSueur的答案对于 socket.io >= 1.0 是正确的。

当我使用 socket.io 0.9 时,我在文档中找到了旧方法。

// in 0.9
var socket = io.connect('localhost:3000', {
  'resource': 'path/to/socket.io';
});

// in 1.0
var socket = io.connect('localhost:3000', {
  'path': '/path/to/socket.io';
});

请注意,a/在新path选项中显示为第一个字符。

于 2014-09-30T15:57:53.540 回答