Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让socket.io为子目录运行_Socket.io_Subdirectory_Socket.io 1.0 - Fatal编程技术网

如何让socket.io为子目录运行

如何让socket.io为子目录运行,socket.io,subdirectory,socket.io-1.0,Socket.io,Subdirectory,Socket.io 1.0,我已经运行了一个代理,它只会在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=1410

我已经运行了一个代理,它只会在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.....
我必须在客户端和服务器上配置什么吗?

在我的服务器上,我必须配置

var io = require('socket.io')(httpServer, {path: '/mysubdir/socket.io'})`
在我的客户身上,我不得不

<script src="http://www.example.com/mysubdir/socket.io/socket.io.js"></script>
对于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';
});

请注意,
/
作为新的
路径
选项中的第一个字符出现。

在我的例子中,我使用nginx作为反向代理。我在轮询时遇到404个错误。这就是我的解决方案

节点服务器的url为

在app.js中,我用

var io = require('socket.io')(http, {path: '/subdir/socket.io'});
在我使用的html中

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

Luke.

使用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"
})

var io=require('socket.io')(http,{path:'/mysubdir/socket.io'})-为meReally cool工作,谢谢你的回答。注意,路径中的“/”似乎是必需的。我试过“mysubdir”没有成功,用“/mysubdir”修改后成功了。我成功了,现在套接字请求url看起来不错。但是我发现请求的资源上没有“Access Control Allow Origin”头,errorSocket请求url看起来很好。但请求的资源上没有“Access Control Allow Origin”标头error@DeepakDholiyan您的错误似乎与CORS问题有关。你的javascript托管在与套接字相同的源代码上吗?现在我获得了ERR\u CERT\u权限_INVALID@DeepakDholiyan假设您使用的是socket.IO 0.9,我不确定您在这里是否有详细的问题。也许在另一个堆栈溢出问题中详细说明代码和问题会更有帮助。在国际海事组织,只告诉你所犯的错误会使任何人都很难帮助你。我的问题链接:-
var socket = io.connect('https://example.com/', {
    path: "/mysubdir"
})