[javascript] Websocket onerror-오류 설명을 읽는 방법?

브라우저 기반 멀티 플레이어 게임을 한동안 개발 해왔고 다양한 환경 (클라이언트 사무실, 공용 와이파이 등)에서 다양한 포트 접근성을 테스트하고 있습니다. 한 가지를 제외하고는 모든 것이 잘 진행되고 있습니다. 오류 번호를 읽는 방법을 알 수 없습니다. 또는 onerror 이벤트 수신시 설명.

클라이언트 웹 소켓은 자바 스크립트로 이루어집니다.

예를 들면 :

// Init of websocket
websocket = new WebSocket(wsUri);
websocket.onerror = OnSocketError;
...etc...

// Handler for onerror:
function OnSocketError(ev)
{
    output("Socket error: " + ev.data);
}

‘출력’은 div에 쓰는 유틸리티 함수입니다.

내가 얻는 것은 ev.data에 대해 ‘정의되지 않음’입니다. 항상. 그리고 나는 인터넷 검색을 해왔지만이 이벤트에 어떤 매개 변수가 있고 올바르게 읽는 방법에 대한 사양이없는 것 같습니다.

도움을 주시면 감사하겠습니다!



답변

핸들러가 수신 하는 오류 Event는 다음 과 같은 정보를 포함하지 않는 간단한 이벤트입니다 .onerror

사용자 에이전트가 WebSocket 연결에 실패해야하거나 WebSocket 연결이 편견으로 종료 된 경우 WebSocket 개체에서 error라는 간단한 이벤트를 발생시킵니다.

실제로 RFC 6455 11.7 에 따른 숫자 코드 와 문자열 속성 을 포함하는 속성 close이있는 이벤트를 수신하면 더 나은 운이있을 수 있습니다 .CloseEventCloseEvent.codeCloseEvent.reason

그러나 CloseEvent.code(및 CloseEvent.reason) 네트워크 검색 및 기타 보안 문제를 방지하는 방식으로 제한 됩니다.


답변

nmaier의 답변과 함께 그는 항상 코드 1006을 받게 될 것이라고 말했습니다 . 그러나 이론적으로 다른 코드를받은 경우 결과를 표시하는 코드가 있습니다 ( RFC6455 를 통해 ).

이 코드는 실제로는 거의 얻을 수 없으므로이 코드는 거의 의미가 없습니다.

var websocket;
if ("WebSocket" in window)
{
    websocket = new WebSocket("ws://yourDomainNameHere.org/");

    websocket.onopen = function (event) {
        $("#thingsThatHappened").html($("#thingsThatHappened").html() + "<br />" + "The connection was opened");
    };
    websocket.onclose = function (event) {
        var reason;
        alert(event.code);
        // See http://tools.ietf.org/html/rfc6455#section-7.4.1
        if (event.code == 1000)
            reason = "Normal closure, meaning that the purpose for which the connection was established has been fulfilled.";
        else if(event.code == 1001)
            reason = "An endpoint is \"going away\", such as a server going down or a browser having navigated away from a page.";
        else if(event.code == 1002)
            reason = "An endpoint is terminating the connection due to a protocol error";
        else if(event.code == 1003)
            reason = "An endpoint is terminating the connection because it has received a type of data it cannot accept (e.g., an endpoint that understands only text data MAY send this if it receives a binary message).";
        else if(event.code == 1004)
            reason = "Reserved. The specific meaning might be defined in the future.";
        else if(event.code == 1005)
            reason = "No status code was actually present.";
        else if(event.code == 1006)
           reason = "The connection was closed abnormally, e.g., without sending or receiving a Close control frame";
        else if(event.code == 1007)
            reason = "An endpoint is terminating the connection because it has received data within a message that was not consistent with the type of the message (e.g., non-UTF-8 [http://tools.ietf.org/html/rfc3629] data within a text message).";
        else if(event.code == 1008)
            reason = "An endpoint is terminating the connection because it has received a message that \"violates its policy\". This reason is given either if there is no other sutible reason, or if there is a need to hide specific details about the policy.";
        else if(event.code == 1009)
           reason = "An endpoint is terminating the connection because it has received a message that is too big for it to process.";
        else if(event.code == 1010) // Note that this status code is not used by the server, because it can fail the WebSocket handshake instead.
            reason = "An endpoint (client) is terminating the connection because it has expected the server to negotiate one or more extension, but the server didn't return them in the response message of the WebSocket handshake. <br /> Specifically, the extensions that are needed are: " + event.reason;
        else if(event.code == 1011)
            reason = "A server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request.";
        else if(event.code == 1015)
            reason = "The connection was closed due to a failure to perform a TLS handshake (e.g., the server certificate can't be verified).";
        else
            reason = "Unknown reason";

        $("#thingsThatHappened").html($("#thingsThatHappened").html() + "<br />" + "The connection was closed for reason: " + reason);
    };
    websocket.onmessage = function (event) {
        $("#thingsThatHappened").html($("#thingsThatHappened").html() + "<br />" + "New message arrived: " + event.data);
    };
    websocket.onerror = function (event) {
        $("#thingsThatHappened").html($("#thingsThatHappened").html() + "<br />" + "There was an error with your websocket.");
    };
}
else
{
    alert("Websocket is not supported by your browser");
    return;
}

websocket.send("Yo wazzup");

websocket.close();

http://jsfiddle.net/gr0bhrqr/ 참조


답변