我有一个项目在同一页面上使用多个HTML5 websocket连接。当所有连接都可用时,页面将按预期运行。但是,当一个或多个websocket连接断开时,所有剩余的连接都被阻塞,直到连接不良为止。超时后,剩余的websockets按预期连接。
请注意下面的uris
数组。我故意添加了一个错误的websocket地址来重新创建这个问题。当each
循环触发时,第一个uri立即连接并更新html中的li
标记。然后浏览器挂起第二个(坏)uri长达60秒,最后移动到第三个uri也立即连接。
我可以在此处重新创建此问题:http://jsfiddle.net/eXZA6/2/
的Javascript
var uris = {
'1': 'ws://echo.websocket.org/',
'2': 'ws://echo.websocket.org:1234/', //Bad websocket address
'3': 'ws://echo.websocket.org/'
};
var sockets = {};
$.each(uris, function(index, uri) {
sockets[index] = connect(index, uri);
});
function connect(index, uri) {
var websocket = new WebSocket(uri);
websocket.onopen = function (evt) {
$('li#' + index).text('Connected');
};
websocket.onclose = function (evt) {
$('li#' + index).text('Closed');
};
websocket.onmessage = function (evt) {
$('li#' + index).text('Received: ' + evt.data)
};
websocket.onerror = function (evt) {
$('li#' + index).text('Error');
};
return websocket;
}
HTML
<ul id="connection">
<li id="1" />
<li id="2" />
<li id="3" />
</ul>
答案 0 :(得分:1)
听起来像Firefox和Chrome遵循WebSocket规范RFC6455中列出的规则,而IE10则没有。
Section 4.1:客户要求:
2. If the client already has a WebSocket connection to the remote
host (IP address) identified by /host/ and port /port/ pair, even
if the remote host is known by another name, the client MUST wait
until that connection has been established or for that connection
to have failed. There MUST be no more than one connection in a
CONNECTING state. If multiple connections to the same IP address
are attempted simultaneously, the client MUST serialize them so
that there is no more than one connection at a time running
through the following steps.
这基本上表示您遇到的行为是websocket客户端所需的行为,以符合规范。
请注意使用“必须”这个词,这是规范文档中一个重要且定义明确的关键词。这个关键词以及其他关键词在Section 2: Conformance Requirements中特别提到。