javascript 更改 Socket.IO 静态文件服务路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12824612/
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
Change Socket.IO static file serving path
提问by Alex Turpin
I am using Socket.IO on a Node server with a basic HTTP server (no Express or Connect or anything like that). By default, Socket.IO serves the client file to
我在带有基本 HTTP 服务器(没有 Express 或 Connect 或类似的东西)的 Node 服务器上使用 Socket.IO。默认情况下,Socket.IO 将客户端文件提供给
/socket.io/socket.io.js
/socket.io/socket.io.js
I would like to be able to change that base path to something else, like
我希望能够将该基本路径更改为其他内容,例如
/foo/bar/socket.io/socket.io.js
/foo/bar/socket.io/socket.io.js
Is there any built-in way to do this, or any way without changing Socket.IO's code? I think the answer lies in the Static
module (require('socket.io').Static)
), but short of writing my own to replace the default, I see no way to go and change the way that behaves.
有没有内置的方法可以做到这一点,或者有什么不改变 Socket.IO 代码的方法?我认为答案就在Static
模块 ( require('socket.io').Static)
) 中,但由于没有编写自己的模块来替换默认值,我认为没有办法改变其行为方式。
How can I do this?
我怎样才能做到这一点?
回答by josh3736
The resource
optionallows you to configure socket.io's root directory. It defaults to /socket.io
.
该resource
选项允许您配置 socket.io 的根目录。它默认为/socket.io
.
var io = require('socket.io').listen(app, { resource: '/foo/bar/socket.io' });
Note that this setting also affects where socket.io's endpoints are served from, so you must also change this setting in your client code.
请注意,此设置还会影响 socket.io 端点的服务来源,因此您还必须在客户端代码中更改此设置。
var socket = io.connect('http://example.com', { resource: 'foo/bar/socket.io' });
(Note we don't use a leading slash here for some reason.)
(请注意,出于某种原因,我们在这里不使用前导斜杠。)
回答by Link
For socket.io version 1.2.1, this works for me.
对于 socket.io 版本1.2.1,这对我有用。
Server side:
服务器端:
var io = require('socket.io')({path: '/foo/bar'});
Client side:
客户端:
var socket = io.connect('http://example.com', {path: '/foo/bar'});
FYI: http://socket.io/docs/migrating-from-0-9/#configuration-differences
仅供参考:http: //socket.io/docs/migrating-from-0-9/#configuration-differences
回答by Tozu
If you are using socket.io version 1.0, the configuration is different than in previous versions.
如果您使用的是 socket.io版本 1.0,则配置与以前的版本不同。
For client side:
对于客户端:
var socket = io.connect('http://localhost:8888', { path: '/some/path/socket.io' });
For server side
对于服务器端
var socket = require("socket.io")( { resource: '/some/path/socket.io' });
回答by zemirco
You can find the client side script socket.io.js
in the path node_modules/socket.io/node_modules/socket.io-client/dist
. Copy it to a new folder and call it with the correct path from the client
您可以socket.io.js
在路径中找到客户端脚本node_modules/socket.io/node_modules/socket.io-client/dist
。将其复制到一个新文件夹并使用客户端的正确路径调用它
<script src="/your/path/to/socket.io.js"></script>
For more configuration visit the wiki
更多配置请访问wiki