Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ws.message is undefined Error, Web Socket response

I am python developer but i need to see my websocket output from browser.

function startSocket() {
    var ws = new WebSocket("ws://localhost:8765/")
    ws.onopen = function(event) {
        ws.send("Sent this from client.js")
        console.log(typeof ws.message);
        console.log("test")
        console.log(ws.message)
    }
}
startSocket();

this is my clien.js file.

<!DOCTYPE html><html lang="en">
  <head>
<!--    <link rel="stylesheet" type="text/css" href="style.css">-->
    <link type="text/css" href="styles.css">
    <meta charset="utf-8">

  </head>
  <body>
    <script src="client.js"></script>

</body></html>

and this is my html file.
I am sending a string from my web socket and i want to see inside of a browser.
When i run this html, my web socket is starting to work but my logs are saying undefined.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is my Log Output

client.js:6 undefined
client.js:7 test
client.js:8 undefined

>Solution :

read more here

bind to the correct event handlers

function startSocket() {
    var ws = new WebSocket("ws://localhost:8765/");
    ws.onopen = function(event) {
        console.log("connection opened");
        ws.send("Sent this from client.js")
    };
    ws.onmessage = function(message) {
        console.log(typeof message);
        console.log("test")
        console.log(message)
    };
};
startSocket();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading