2017-12-06 22 views
0

私の接続数を超えない限り、websocketコードは正常に動作します。 503エラーが発生したときにトラップしたい。表示されるコンソール出力は次のようになります。javascriptでwebsocket 503 max connectionsをトラップする方法はありますか?

SCRIPT12008: WebSocket Error: Incorrect HTTP response. Status code 503, Number of active WebSocket requests has reached the maximum concurrent WebSocket requests allowed.

WebSocket.OnErrorハンドラは詳細を表示しません。上記のコンソール出力に示されているように詳細レベルを取得するにはどうすればよいですか?何らかのドキュメントコレクションや、まあ、私は本当にわからない。

ここで完全なコードです。ソケットサーバーは生きており、意図的に12/6現在4つの接続に制限されています。後でそれを切り詰めるだろう。それが失敗したときに発生

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8" /> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<link rel="stylesheet" href="styles/base.css" /> 
<title>Yielding Loop</title> 
<script type="text/javascript"> 
    var c = 0; 
    var t; 
    var timer_is_on = 0; 
    var m_connection = null; 
    var m_socket_address = "ws://edgeplay.azurewebsites.net/ws"; 

    function timedCount() 
    { 
     echoTest(); 
     document.getElementById("txt").value = c; 
     outmsg("tick=" + c); 
     c = c + 1; 
     t = setTimeout(function() { timedCount() }, 3000); 
    } 

    function startCount() 
    { 
     if (!timer_is_on) 
     { 
      timer_is_on = 1; 
      timedCount(); 
     } 
    } 

    function stopCount() 
    { 
     clearTimeout(t); 
     timer_is_on = 0; 
    } 

    function SocketInit() 
    { 
     try 
     { 
      if ("WebSocket" in window) 
      { 
       outmsg("WebSocket supported, gtg."); 
      } 
      else 
      { 
       outmsg("WebSocket NOT SUPPORTED, bailing out"); 
       return 0; 
      } 

      outmsg("connecting to " + m_socket_address); 

      try 
      { 
       m_connection = new WebSocket(m_socket_address); 
      } 
      catch (err) 
      { 
       outmsg("error creating new socket - " + err); 
       outmsg("error.name:" + err.name); 
       outmsg("error.message:" + err.message); 
      } 

      m_connection.onopen = function (openEvent) 
      { 
       outmsg("WebSocket.OnOpen - GTG: " + m_socket_address); 
      }; 

      m_connection.onmessage = function (messageEvent) 
      { 
       outmsg('WebSocket.onmessage - Server Reply:: ' + messageEvent.data); 
      }; 

      m_connection.onerror = function (errorEvent) 
      { 
       outmsg("WebSocket Status:: Error was reported."); 
       outmsg("WebSocket errorEvent: " + errorEvent); 
       outmsg("Stopping count"); 
       var h1 = document.getElementById("h1Main"); 
       h1.style = "background-color:red;"; 
       stopCount(); 
      }; 

      m_connection.onclose = function (closeEvent) 
      { 
       outmsg("WebSocket Status:: Socket Closed"); 
       outmsg("closeEvent.code:" + closeEvent.code); 
       outmsg("closeEvent.data:" + closeEvent.data); 
      }; 
     } 
     catch (exception) 
     { 
      outmsg("exception: " + exception); 
     } 
    } 

    var m_input = null; 
    var m_querystring = null; 
    function echoTest() 
    { 
     try 
     { 
      m_input = m_input == null ? document.getElementById("textEcho") : m_input; 
      m_connection.send(m_input.value + location.search); 
     } 
     catch (err) 
     { 
      outmsg("eating error " + err); 
     } 
    } 


    var m_out = null; 
    var outd = null; 
    function outmsg(msg) 
    { 
     m_out = (m_out == null) ? document.getElementById("divOutput") : m_out; 
     outd = new Date(); 
     m_out.innerHTML = outd.toLocaleTimeString() + ": " + msg + "<br/>" + m_out.innerHTML; 
    } 
</script> 
</head> 
<body onload="SocketInit();startCount();"> 
<h1 id="h1Main">Yielding Loop</h1> 
<button onclick="SocketInit();startCount();">Init socket and start count</button> 
<input type="text" id="txt"> 
<button onclick="stopCount()">Stop count!</button> 
<br/> 
<span>text to echo: </span><input type="text" value="BLUE" id="textEcho"/> 

<div id="divOutput" style="background-color:antiquewhite; border:3px; width:900px;"> 
    ready.... 
</div> 
<hr /> 
</body> 
</html> 

エラー:発生したエラーにデバッグ

。それほど良いことではありません。以下の出力ライン。逆のクロノ。

1‎:‎04‎:‎59‎ ‎PM: --- closeEvent.reason: 
‎1‎:‎04‎:‎59‎ ‎PM: --- closeEvent.data:undefined 
‎1‎:‎04‎:‎59‎ ‎PM: --- closeEvent.code:1006 
‎1‎:‎04‎:‎59‎ ‎PM: WebSocket.onClose 
‎1‎:‎04‎:‎59‎ ‎PM: --- Stopping count 
‎1‎:‎04‎:‎59‎ ‎PM: --- errorEvent.type: error 
‎1‎:‎04‎:‎59‎ ‎PM: --- errorEvent: [object Event] 
‎1‎:‎04‎:‎59‎ ‎PM: WebSocket.onerror 
+1

503が発生したとき、正確に「onerror」が表示されますか? – jfriend00

+0

がエラーにデバッグされました。それほど良いことではありません。以下の出力ライン。逆のクロノ。私たちはより良い書式を得るためにQに配置。 1:04:59 PM --- --- closeEvent.reason: 1:04:59 PM --- --- closeEvent.data:undefined 1: 04:59 PM:--- closeEvent.code:1006 1:04:59 PM:WebSocket.onClose 1:04:59 PM :---停止カウント 1:04:59 PM:--- errorEvent.type:エラー 1:04:59 PM:--- errorEvent:[---]オブジェクトイベント] 1:04:59 PM:WebSocket.onerror –

+0

'errorEvent'には何がありますか? – jfriend00

答えて

0

明らかにセキュリティ上の理由から、1006オンクローズを取得することはありません。私の幸せを残していないが、それはRFCが書かれている方法です。 https://html.spec.whatwg.org/multipage/web-sockets.html#network

User agents must not convey any failure information to scripts in a way that would allow a script to distinguish the following situations: A server whose host name could not be resolved. A server to which packets could not successfully be routed. A server that refused the connection on the specified port. A server that failed to correctly perform a TLS handshake (e.g., the server certificate can't be verified). A server that did not complete the opening handshake (e.g. because it was not a WebSocket server). A WebSocket server that sent a correct opening handshake, but that specified options that caused the client to drop the connection (e.g. the server specified a subprotocol that the client did not offer). A WebSocket server that abruptly closed the connection after successfully completing the opening handshake. In all of these cases, the the WebSocket connection close code would be 1006, as required by the WebSocket Protocol specification. [WSP]

関連する問題