Javascript 多个 Websocket
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12498429/
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
Multiple Websockets
提问by Arthur
I'm trying to use two websockets on one page. This is my code:
我试图在一页上使用两个 websocket。这是我的代码:
var pageViewWs = new WebSocket("ws://localhost:9002/pageView");
var sessionWs = new WebSocket("ws://localhost:9002/session");
pageViewWs.onmessage = function (event) {
alert("PageView");
};
sessionWs.onmessage = function (event) {
alert("Session");
};
Only the PageView alert appears. On the server side no requests are made to /session, only to /pageView.
仅显示 PageView 警报。在服务器端,不向 /session 发出请求,只向 /pageView 发出请求。
Now, if I switch var pageViewWs and var sessionWs around then the Session alert is shown instead of the PageView. It is not because they are alerts, I've tried appending to the body and to divs and I've stepped through using Firebug. It seems that only one WebSocket can be created at a time although in Firebug the properties for pageViewWs and sessionWs appear the same with the exception of their url.
现在,如果我切换 var pageViewWs 和 var sessionWs,则会显示会话警报而不是 PageView。这不是因为它们是警报,我已经尝试附加到正文和 div 并且我已经逐步使用 Firebug。似乎一次只能创建一个 WebSocket,尽管在 Firebug 中,pageViewWs 和 sessionWs 的属性看起来是一样的,除了它们的 url。
I've only tested this in Firefox 15.0.1. Is there some sort of Websocket limitation whereby you can only run one at a time? Or is something wrong with my code?
我只在 Firefox 15.0.1 中测试过。是否存在某种 Websocket 限制,您一次只能运行一个?还是我的代码有问题?
回答by logical Chimp
I believe you can only create one WebSocket connection from a client to a specific port on the host. Have you tried either running the two services on different ports, or on different servers? This would allow you to determine the limitation...
我相信您只能创建一个从客户端到主机上特定端口的 WebSocket 连接。您是否尝试过在不同的端口或不同的服务器上运行这两个服务?这将允许您确定限制...
回答by Subin
I faced the same problem to run multiple services through the same port. So, I created a PHP libraryto do this.
我在通过同一个端口运行多个服务时遇到了同样的问题。所以,我创建了一个PHP 库来做到这一点。
Why ?
为什么 ?
Some free plans of hosting providers don't allow you to bind to ports or allow you to bind to one port. In case of OpenShift Cloud Server, you can only bind to port 8080. So, running multiple WebSocket services is not possible. In this case, Francium DiffSocketis useful.
一些托管服务提供商的免费计划不允许您绑定到端口或允许您绑定到一个端口。在OpenShift Cloud Server 的情况下,您只能绑定到端口 8080。因此,无法运行多个 WebSocket 服务。在这种情况下,Francium DiffSocket很有用。
You can run different services on the same port using a PHP librarycalled Francium DiffSocket.
您可以使用名为Francium DiffSocket的PHP 库在同一端口上运行不同的服务。
After setting up Francium DiffSocket, you can do this for using different services :
设置 Francium DiffSocket 后,您可以使用不同的服务执行此操作:
var chatWS = new WebSocket("ws://ws.example.com:8000/?service=chat");
var gameWS = new WebSocket("ws://ws.example.com:8000/?service=game");
An example are these services which are running through a single port :
一个例子是这些通过单个端口运行的服务:
回答by Omran Jamal
Apart from the HTTP Request head both the request are the same. They hit the same application server on the same port. It is up to the server side application to treat each connection differently based on the HTTP request that initiated it.
除了 HTTP 请求头之外,两个请求都是相同的。他们在同一个端口上访问同一个应用程序服务器。服务器端应用程序根据发起它的 HTTP 请求对每个连接进行不同的处理。
I've done this in node. You could do it manually but packages like
我已经在 node.js 中做到了这一点。你可以手动完成,但像这样的包
eases the process.
简化了过程。