Home > Blockchain >  ws.message is undefined Error, Web Socket response
ws.message is undefined Error, Web Socket response

Time:07-21

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.

This is my Log Output

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

CodePudding user response:

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();
  • Related