WebSocket(或 AJAX 长轮询等)是否可以在与原始 HTTP 请求不同的端口上运行?
我想编写一个应用程序,将 WebSockets 与 node.js 和 socket.io 结合使用,并与 Django 结合使用,并通过 Redis 共享数据(这句话中有很多流行语!)。
如果我在不同的端口(例如 5555)上运行节点,我是否能够使用 WebSocket(或使用 socket.io,如长轮询等)或者同源策略会给我带来问题吗?
如果是这样,我该如何绕过它们?
I want to write an application that uses WebSockets with node.js and socket.io in combination with Django and will be sharing data through Redis (lots of buzzwords in that sentence!).
If I run node on a different port (e.g., 5555), will I be able to connect to it with a WebSocket (or fallback method with socket.io, like long-polling, etc.) or will the same origin policy give me problems?
If so, how can I get around them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
socket.io 确实支持跨域连接(请参阅常见问题解答)。不同的端口也可以正常工作。
socket.io does support cross-domain connections (see FAQ). A different port will work fine.
我不确定 WebSockets,尽管 Ajax 会给你带来问题。
我相信可以通过使用服务器端反向代理来解决这个问题。
I am not sure about WebSockets, although Ajax would give you problems with this.
I believe that it could be solved by using a server-sided reverse proxy though.
Socket.io 支持 JSONP 轮询,通常用于跨域长轮询(除了 CORS 并非所有浏览器都支持)。关于 WebSocket 的跨域主题 - 规范 规定下列:
您“应该”能够使用具有不同端口的 WebSocket,事实上您可能别无选择,因为其他 Web 服务器将使用 80 等标准端口来为基于 Django 的应用程序提供服务。特定于浏览器的并行连接也存在潜在问题限制如果您使用相同的域和端口(例如,当您尝试在同一浏览器上下文中打开多个选项卡时,您可以在 Facebook 聊天中看到这一点 - 每个选项卡在长轮询期间都连接到不同的子域克服这些限制)。
Socket.io supports JSONP polling which is commonly used for cross-domain long polling (apart from CORS which is not supported by every browser). On cross-domain subject regarding WebSockets - specification states the following:
You "should" be able to use WebSockets with different port, in fact you probably have no other choice since standard ports like 80 will be used by other web server to serve your Django based application. There will be also potential problems with browser specific parallel connection limits if you use the same domain and port (for example you can see this in Facebook chat when you try to open more than one tab within the same browser context - each tab is connected to different subdomain during long polling to overcome these limits).
答案是“有时”。它非常特定于浏览器和安全策略。
假设您使用的是 Socket.IO...我没有遇到任何问题,除了在 Opera 中它无法绕过 SOP(同源策略)规则。修复方法是启用 WebSocket。
The answer is "sometimes". Its very much browser and security policy specific.
Assuming you're using Socket.IO... I've had no issues, except in Opera where it was unable to bypass the SOP (Same-origin policy) rules. The fix was enabling WebSockets.