更改Socket.IO静态文件服务路径 [英] Change Socket.IO static file serving path
问题描述
我在具有基本HTTP服务器的节点服务器上使用Socket.IO(没有Express或Connect或类似的东西)。默认情况下,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代码的方式?我认为答案在于静态
模块( require('socket.io')。静态)
),但是如果没有写我自己来替换默认值,我认为无法改变行为方式。
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屋!