更改 Socket.IO 静态文件服务路径 [英] Change Socket.IO static file serving path
问题描述
我在带有基本 HTTP 服务器(没有 Express 或 Connect 或类似的东西)的 Node 服务器上使用 Socket.IO.默认情况下,Socket.IO 将客户端文件提供给
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
/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
有没有内置的方法可以做到这一点,或者有什么不改变 Socket.IO 代码的方法?我认为答案在于 Static
模块(require('socket.io').Static)
),但是我没有编写自己的来替换默认值,我明白了没有办法去改变行为方式.
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.
我该怎么做?
推荐答案
resource
选项 允许您配置 socket.io 的根目录.默认为 /socket.io
.
var io = require('socket.io').listen(app, { resource: '/foo/bar/socket.io' });
请注意,此设置还会影响 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.
var socket = io.connect('http://example.com', { resource: 'foo/bar/socket.io' });
(请注意,出于某种原因,我们在此处不使用前导斜杠.)
(Note we don't use a leading slash here for some reason.)
这篇关于更改 Socket.IO 静态文件服务路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!