Chrome WebSocket - onopen is not a function

I have a really simple websocket test on chrome, but it seems to fail:

var ws = new WebSocket('ws://localhost:8002/', 'a')

 ws.onopen(function() {
     console.log("ok")
 })

He says to me Uncaught TypeError: Property 'onopen' of object #<WebSocket> is not a function. I would suggest that onopen should exist as a method, regardless of whether the websocket server really works, but I have one run on this port.

I am using chrome 32.0.1700. I see that all callback methods (onopen, onmessage, etc.) are null. What's going on here?

+4
source share
1 answer

The function is incorrectly assigned to the onopen event. Do it like this:

var ws = new WebSocket('ws://localhost:8002/', 'a')

ws.onopen = function() {
  console.log("ok")
};

http://www.tutorialspoint.com/html5/html5_websocket.htm

+7
source

Source: https://habr.com/ru/post/1531121/


All Articles