By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – How can I get Websocket error code in javascript
JavaScript

Javascript – How can I get Websocket error code in javascript

Admin
Last updated: 2024/02/10 at 2:04 PM
Admin
Share
2 Min Read
How can I get Websocket error code in javascript

Problem:

My program tries to create websocket and generates an error.
The code is:

Contents
Problem:Solution:
            let errMsg = "";
            try {
                socket = new WebSocket(server_ip.value, "chat");    // (*)
            }
            catch(err) {
// js doesn't come to this branch
                Websocketerror = true;
                errMsg = "Websocket creation error. Code = " + err.code.toString() +
                            "  Description = _" + err.message + "_";
            }

            if (websocketError) {
                appendMessageText(errMsg, "msg_err");
            }
            else {
                socket.onopen = ws_OnConnectionCreated;
                socket.onclose = ws_onConnectionClose;
                socket.onerror = ws_OnConnectionError;
            }

function ws_onConnectionClose(event) {

        let status = event.wasClean ? "ok." : "(aborted).";

        appendMessageText("Websocket is closed" + status, "msg_log");
        console.log("------> ws_onConnectionClose(event): event =", event);

};

I see an error message in console: “Websocket connection to …ip… failed: Error during WebSocket handshake: Unexpected response code: 499”. It refers to the source line (*)
It occurs when antivirus is active – I guess it blocks websocket-connection and forces returning the code = 499.
My handler function ws_onConnectionClose(event) was called, but I don’t see – from which variable I can get error message (I have searched it in event by console.log(event)) ?

The question: how can I get error’s content (error message, described above) ?

Solution:

Short answer: You can’t.

For security reasons the detailed debug info is not available to JS. You wouldn’t want me to be able to put some javascript on my public website that probes your local network and sends back detailed information about the connection attempts.

That being said, you can still go by event.wasClean and event.code to display a generic error. The error codes are defined in RFC 6455, but from my experience it’s almost always code 1006 which is being returned. Also from my experience, the event.reason property is almost always an empty string.

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article How to properly implement getter/setter functionality, either for both a property's `get` and `set`, or for two methods and a property's `get`? Javascript – How to properly implement getter/setter functionality, either for both a property’s `get` and `set`, or for two methods and a property’s `get`?
Next Article Update Input Field Based on Image Click Javascript – Update Input Field Based on Image Click
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?