0

同じページで複数の 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 では動作しません。
4

1 に答える 1

1

Firefox と Chrome は WebSocket Spec RFC6455で概説されているルールに従っているようですが、IE10 はそうではありません。

セクション 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 クライアントに必要な動作であることを示しています。

「MUST」という言葉の使用に注意してください。これは、仕様ドキュメントで重要かつ明確に定義されたキーワードです。このキーワードは、他のキーワードとともに、セクション 2: 適合要件で具体的に示されています。

于 2013-10-09T20:54:30.587 に答える