Sockjs with Angularjs - Error: INVALID_STATE_ERR

I am trying Angularjs with sockjs and I am using this to help: https://github.com/bendrucker/angular-sockjs

I would like to send some data via sockjs on page load, but when trying to make the following call: Error: INVALID_STATE_ERR .

socket.send("test data");

      

I assume this is because Sockjs is not "ready" yet. If I package it in $timeout

, it works if the delay is long enough. 1000 works, but 100 doesn't.

$timeout(function() {
  socket.send("test");
}, 1000);

      

This is obviously a hack and I wondered how I can detect when sockjs is ready so I can make a call.

+3


source to share


1 answer


You have to use a method setHandler

and provide a callback for 'open'

. It will be safe to send messages there. Hauling quite easy, and is intended to ensure that the three inverse customer call SockJS ( onopen

, onclose

, onmessage

) were caused within the loop digest. If you wanted, you could easily use it to create a more complex service, which could be among the many possibilities:



  • queues send messages when the socket is closed and send them when it is opened
  • returns a $q

    promise which resolves when the socket is opened
+3


source







All Articles