Websocket onerror - त्रुटि विवरण कैसे पढ़ें?


79

मैं कुछ समय के लिए ब्राउज़र-आधारित मल्टी प्लेयर गेम विकसित कर रहा हूं और मैं विभिन्न वातावरण (क्लाइंट के कार्यालय, सार्वजनिक वाईफाई आदि) में विभिन्न बंदरगाहों की पहुंच का परीक्षण कर रहा हूं। सभी बहुत अच्छी तरह से जा रहे हैं, सिवाय एक बात के: मैं यह पता नहीं लगा सकता कि त्रुटि को कैसे पढ़ें। या वर्णन जब onerror घटना प्राप्त होती है।

क्लाइंट वेबस्कैट को जावास्क्रिप्ट में किया जाता है।

उदाहरण के लिए:

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

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

'आउटपुट' सिर्फ कुछ यूटिलिटी फंक्शन है जो डिव में लिखते हैं।

मुझे जो मिल रहा है वह ev.data के लिए 'अपरिभाषित' है। हमेशा। और मैं चारों ओर घूम रहा हूं, लेकिन ऐसा लगता है कि इस घटना के बारे में कोई चश्मा नहीं है और इसे ठीक से कैसे पढ़ा जाए।

किसी भी मदद की सराहना की है!


2
ध्यान दें, यह डिज़ाइन-बाय है कि आप वेबसोकेट से उपयोगी त्रुटि की जानकारी प्राप्त नहीं कर सकते हैं: stackoverflow.com/a/31003057/771768
कार्ल वॉल्श

जवाबों:


75

त्रुटि हैंडलर प्राप्त करता है एक साधारण इस तरह की जानकारी से युक्त नहीं घटना है :Eventonerror

यदि WebSocket कनेक्शन को विफल करने के लिए उपयोगकर्ता एजेंट की आवश्यकता होती है या WebSocket कनेक्शन को पूर्वाग्रह से बंद कर दिया जाता है, तो WebSocket ऑब्जेक्ट में त्रुटि नाम की एक साधारण घटना को आग लग जाती है।

आपके पास closeईवेंट के लिए सुनने के लिए बेहतर भाग्य हो सकता है , जो कि एक है CloseEventऔर वास्तव में आरएफसी 6455 11.7 और एक स्ट्रिंग प्रॉपर्टी CloseEvent.codeके अनुसार संख्यात्मक कोड वाली संपत्ति है।CloseEvent.reason

कृपया ध्यान दें, यह CloseEvent.code(और CloseEvent.reason) इस तरह से सीमित है कि नेटवर्क जांच और अन्य सुरक्षा मुद्दों से बचा जाता है।


5
मैं क्रोम का उपयोग कर रहा हूं और इसका कारण "" है, अजीब बात यह है कि F12 डेवलपर्स टूल बहुत अधिक जानकारी देता है।
डॉ.वाईएसजी

4
@ Dr.YSG यह बिल्कुल भी अजीब नहीं है। डेवलपर टूल में त्रुटि संदेश ब्राउज़र के उपयोगकर्ता के लिए है, इसलिए इसमें संवेदनशील जानकारी हो सकती है। OTOH कुछ यादृच्छिक जावास्क्रिप्ट कोड आमतौर पर विश्वसनीय नहीं है। अन्यथा आप एक कीड़ा (पोर्ट स्कैनर या डीडीओएस स्क्रिप्ट की तरह) लिख सकते हैं और बस कुछ यादृच्छिक विज्ञापन नेटवर्क के माध्यम से इसे फैला सकते हैं।
जेपीसी

88

नेमार के जवाब के साथ, जैसा कि उन्होंने कहा कि आपको हमेशा कोड 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/ देखें


2
वे घटना कोड काम नहीं करेंगे, आप आमतौर पर केवल 1006 कोड प्राप्त कर सकते हैं।
बेनामी

2
हाँ, मेरा जवाब वास्तव में काल्पनिक है जब तक कि वे कुछ अजीब स्थिति में न हों जहां वे अधिक कोड प्राप्त कर सकते हैं, मुझे यकीन नहीं है कि मेरे पास इतने सारे उत्थान हैं क्योंकि मैंने उल्लेख किया है कि मेरे उत्तर में सबसे ऊपर है
Phylliida
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.