1

I'm trying to connect to a server using websockets. I've set up the port on the server, and am trying to connect through the Dev console in Chrome (also tried Firefox and got the same result).

I connect from the console using:

var websocket = new WebSocket('ws://localhost:5001');

This gives me the message "undefined". However, if I do

websocket.readyState

I get "1".

I then do

websocket.binaryType = 'arraybuffer';

which prints "arraybuffer".

If I then do something like

websocket.send("1+1");

it says undefined.

However, if I do all of this in an HTML file with JavaScript, it connects fine and I get the result "2", so it looks as though the Websocket itself is ok, and what I'm typing in is ok, but it's something to do with it being in the Dev Console that's the issue.

I don't know anything much about setting up Websockets.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sharon
  • 3,471
  • 13
  • 60
  • 93
  • 1
    related: https://stackoverflow.com/questions/14633968/chrome-firefox-console-log-always-appends-a-line-saying-undefined – Darren Smith Nov 20 '19 at 16:27
  • Thank you! That helped - I added an onmessage function which prints the result to the console and now it displays. – Sharon Nov 21 '19 at 19:55

1 Answers1

1

I solved this by adding an onmessage function to the websocket:

websocket.onmessage = function (result) {
console.log(result.data);
}
Sharon
  • 3,471
  • 13
  • 60
  • 93