Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome WebSocket - onopen is not a function

I have a really simple websocket test on chrome, but it seems to be failing miserably:

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

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

It tells me: Uncaught TypeError: Property 'onopen' of object #<WebSocket> is not a function. I would assume that onopen should exist as a method whether or not there is a websocket server actually running, but I do have one running on that port.

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

like image 418
B T Avatar asked Mar 11 '14 08:03

B T


1 Answers

The function is not correctly assigned to the onopen event. Do it like this instead:

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

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

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

like image 187
Johan Avatar answered Sep 30 '22 07:09

Johan