Javascript 使用特定路径和命名空间连接到 Socket.IO 服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29511404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connect to Socket.IO server with specific path and namespace
提问by guidoman
My Node.js application is running at URL http://www.example.com/myapp/.
我的 Node.js 应用程序在 URL http://www.example.com/myapp/上运行。
I have configured a Socket.IO server (version 1.3.5) with a custom namespace. Here is an example code snippet:
我已经使用自定义命名空间配置了 Socket.IO 服务器(版本 1.3.5)。这是一个示例代码片段:
var server = http.createServer(...);
var io = socketio(server);
io
.of('/a/b/c')
.on('connection', function (socket) {
socket.emit('update', {msg: '/a/b/c'});
});
I can't figure out how to connect to this service from the client. My guesses (none of these is working):
我不知道如何从客户端连接到这个服务。我的猜测(这些都不起作用):
io.connect('http://www.example.com/myapp/a/b/c');
io.connect('http://www.example.com', {path: '/myapp/a/b/c'});
io.connect('', {path: '/myapp/a/b/c'});
io.connect('http://www.example.com/a/b/c', {path: '/myapp'});
io.connect('http://www.example.com', {path: '/myapp/socket.io/a/b/c'});
回答by pieterjandesmedt
On your server, don't forget to specify the path as well:
在您的服务器上,也不要忘记指定路径:
var io = require('socket.io')(http, { path: '/myapp/socket.io'});
io
.of('/my-namespace')
.on('connection', function(socket){
console.log('a user connected with id %s', socket.id);
socket.on('my-message', function (data) {
io.of('my-namespace').emit('my-message', data);
// or socket.emit(...)
console.log('broadcasting my-message', data);
});
});
On your client, don't confuse namespace and path:
在您的客户端上,不要混淆命名空间和路径:
var socket = io('http://www.example.com/my-namespace', { path: '/myapp/socket.io'});
回答by Wojtek
I'm using 1.3.5 too, in a slightly similar scenario, from an Angular single page app, where the client code for socket.io is just concatenated with the rest of the app (from a bower package), rather than downloaded/included from a particular network location.
我也在使用 1.3.5,在一个稍微类似的场景中,来自 Angular 单页应用程序,其中 socket.io 的客户端代码只是与应用程序的其余部分(来自 bower 包)连接,而不是下载/来自特定网络位置。
What seems to work for me in the setup where my socket.io is at:
在我的 socket.io 所在的设置中,什么似乎对我有用:
http://somedomain.com:9096/sockets/socket.io.js
rather than the default:
而不是默认值:
http://somedomain.com:9096/socket.io/socket.io.js
(I manually adjusted the path on the server side), is:
(我在服务器端手动调整了路径),是:
io.connect('http://somedomain.com:9096' + '/' + namespaceName, { path: '/sockets' });
It looks equivalent to your scenario:
它看起来相当于你的场景:
io.connect('http://www.example.com/a/b/c', {path: '/myapp'});
which may be worth another try. I haven't fully tested the namespaceNamewith forward slashes in it, but it seems to pick up the connection on the client side, when I simply change my namespace to '/a/b/c'
这可能值得再试一次。我还没有完全测试namespaceName其中的正斜杠,但是当我简单地将命名空间更改为时,它似乎在客户端获取了连接'/a/b/c'
What probably makes the difference is my server side setup, which goes:
可能与众不同的是我的服务器端设置,它是:
var server = http.createServer(app);
var io = require('socket.io')(server, { path: '/sockets' }).listen(server);
My answer is more of a general indication that it is possibleto use both a namespace and a customised path, despite the setup being unobvious. I hope it may be useful for you in some way.
我的回答更像是一个普遍的迹象,即可以同时使用命名空间和自定义路径,尽管设置并不明显。我希望它在某种程度上对你有用。
回答by Filip Dupanovi?
You can check the official documentation on Rooms and Namespaces. Basically, the great thing about socket.iois that, once your client requests the client-side sources, it will transmit all the necessary details required for the client to connect to the server (host, path, port, etc.).
您可以查看有关Rooms 和 Namespaces的官方文档。基本上,最重要的socket.io是,一旦您的客户端请求客户端源,它将传输客户端连接到服务器所需的所有必要详细信息(主机、路径、端口等)。
To connect to your specific namespace, on the client you would simply have to specify:
要连接到您的特定命名空间,您只需在客户端指定:
var socket = io('/a/b/c');

