Socket.IO中的跨域连接
是否可以跨域方式使用Socket.IO?如果是这样,怎么办?网络上提到了这种可能性,但没有给出任何代码示例。
Is it possible to use Socket.IO in a cross domain manner? If so, how? The possibility is mentioned around the web but no code examples are given anywhere.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
引用 socket.io 常见问题解答:
至于它是如何做到的:本机 WebSocket 在设计上是跨域的,socket.io 为跨域闪存通信提供闪存策略文件,XHR2 可以使用 CORS,最后您始终可以使用 JSONP。
Quoting the socket.io FAQ:
As to how it does it: Native WebSockets are cross-domain by design, socket.io serves a flash policy file for cross-domain flash communication, XHR2 can use CORS, and finally you can always use JSONP.
**Socket.IO版本--> 1.3.7 **
是否可以跨域方式使用Socket.Io?
是的,绝对是。
如果是,如何实现?
选项 1:仅强制使用 Websocket
默认情况下,Websocket 是跨域的。如果您强制 Socket.io 仅使用它作为连接客户端和服务器的方式,那么您就可以开始了。
服务器端
客户端
就是这样。问题?不适用于不支持 websocket 的浏览器(对于客户端)。这样你就几乎扼杀了 Socket.io 的魔力,因为它逐渐从长轮询开始,然后升级到 websockets(如果客户端支持它)。
如果你 100% 确定所有客户端都将使用 HTML5 兼容的浏览器进行访问,那么你就可以走了。
选项 2:在服务器端允许 CORS,让 Socket.io 处理是使用 websockets 还是长轮询。
对于这种情况,您只需调整服务器端设置即可。客户端连接与往常相同。
服务器端
就是这样。希望它对其他人有帮助。
**Socket.IO version --> 1.3.7 **
Is it possible to use Socket.Io in a cross domain manner?
Yes, absolutely.
If so, how?
Option 1: Force use of Websockets only
By default, websockets are cross domain. If you force Socket.io to only use that as means to connect client and server, you are good to go.
Server side
Client side
That's it. Problem? Won't work on browsers (for clients) who don't support websockets. With this you pretty much kill the magic that is Socket.io, since it gradually starts off with long polling to later upgrade to websockets (if client supports it.)
If you are 100% sure all your clients will access with HTML5 compliant browsers, then you are good to go.
Option 2: Allow CORS on server side, let Socket.io handle whether to use websockets or long polling.
For this case, you only need to adjust server side setup. The client connection is same as always.
Server side
That's it. Hope it helps anyone else.
只需在创建客户端套接字时插入您的远程域名:
一些 MDN 上的文档
但是,您应该确保您的服务器接受 Websocket(配置和标头)。
旧版本:
Simply insert your remote domain name when creating the client side socket:
Some documentation on MDN
You should however ensure that your server accepts websockets (configuration and headers).
Old version :
Socket.io 支持跨域连接,但请记住,您的 cookie 不会传递到服务器。您必须:
(1) 提出一个备用标识方案(自定义令牌或 javascript cookie - 请记住,这不应该是实际的会话 ID,除非您想让自己面临会话风险劫持)
或(2)首先向服务器发送一个好的老式 HTTP JSONP 请求以获取 cookie。然后它将通过套接字连接握手进行传输。
Socket.io supports cross-domain connections, but keep in mind that your cookie will not be passed to the server. You'll have to either:
(1) come up with an alternate identification scheme (a custom token or a javascript cookie-- just keep in mind this should not be the actually session id, unless you want to put yourself at risk of session hijacking)
or (2) send a good old fashioned HTTP JSONP request to the server first to get the cookie. Then it will be transmitted w/ the socket connection handshake.
通过 io 创建服务器,如下所示:
在 origins 数组中指定有效的 origin
create your server by io like this:
in the origins array specify valid origin
简单又安全!
在主文件中将其放在 io.on('connection') 之前,添加以下行:
Easy and security!
In the main file place it before io.on('connection'), add the lines:
是的,确实如此。
我实现了跨域socket.io来测试它是否有效。
那应该可以正常工作。
Yes it does.
I have implemented cross domain socket.io to test whether it works.
That should work fine.
从 v4 开始,根据 文档:
您可以在传递给
new Server()
的配置对象中使用cors
键。请参阅上述文档页面中的基本示例:origin
可以是接受多个来源的数组。其他选项包括
allowRequest
、allowedHeaders
、withCredentials
、extraHeaders
以及cors
包中的其他内容最终在后台处理 CORS 标头。我还测试了与 这个答案 稍有不同的语法,它适用于 4.4.1:
As of v4, according to the docs:
You can use the
cors
key in the configuration object passed tonew Server()
. See this basic example from the above docs page:origin
can be an array to accept multiple origins.Other options include
allowRequest
,allowedHeaders
,withCredentials
,extraHeaders
and others from thecors
package which ultimately handles the CORS headers under the hood.I also tested slightly different syntax than this answer, which worked for me with 4.4.1: