同じページで複数の HTML5 websocket 接続を使用するプロジェクトがあります。すべての接続が利用可能になると、ページは期待どおりに機能します。ただし、1 つ以上の Websocket 接続がダウンしている場合、残りのすべての接続は、問題のある接続がタイムアウトになるまでブロックされます。タイムアウト後、残りの Websocket は期待どおりに接続します。
uris
以下の配列に注意してください。この問題を再現するために、意図的に不正な WebSocket アドレスを追加しました。ループが発生するeach
と、最初の uri がすぐに接続さli
れ、html 内のタグが更新されます。その後、ブラウザーは 2 番目 (不良) の uri で 60 秒間ハングアップした後、最終的に 3 番目の 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>
- setTimeout やその他のハッキーなマルチスレッド トリックを使用してみましたが、うまくいきませんでした。
- 奇妙なことに、私が期待していた機能は IE10 では動作するように見えますが、Firefox や Chrome では動作しません。