Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InvalidStateError on websocket object

I'm just starting learning websockets, andI get a strange error:

<script type="text/javascript">
    window.onload = function(){
        var service = new WebSocket("ws://localhost:8080/websocket/test"); 
        service.onmessage = function(event){
            alert("message"); 
        } 
        service.onopen = function(){
            service.send("hello!");
        } 
        service.onclose = function(){
            alert("closed"); 
        } 
        service.onerror = function(){
            alert("error"); 
        }

        service.send("test");
        service.close();
    }

</script>

on the line:

            service.send("test");

I get:

InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable

Am I missing something important?

like image 838
Stefano Vercellino Avatar asked Jun 16 '16 11:06

Stefano Vercellino


People also ask

How do I fix a WebSocket error?

Solution 1Check that all the Bot Insight services are running. Check that your firewall settings are configured to accept incoming websocket data. Try to use a different web browser. Restart the Bot Insight Visualization and Bot Insight Scheduler services.

Can WebSockets send binary data?

Data Types and ExtensionsWebSockets support sending binary messages, too. To send binary data, one can use either Blob or ArrayBuffer object. Instead of calling the send method with string, you can simply pass an ArrayBuffer or a Blob .

When using WebSocket send () How do you know when?

The WebSocket. send() method enqueues the specified data to be transmitted to the server over the WebSocket connection, increasing the value of bufferedAmount by the number of bytes needed to contain the data.

What data can be transferred on WebSocket?

Unlike the HTTPS protocol, WebSocket allows for data transfer in both directions. WebSockets can be used to build out anything from a real-time trading application to a chat application, or just about anything that requires efficient, real-time data transfer.


2 Answers

The WebSocket.onopen property is an EventHandler that is called when the WebSocket connection's readyState changes to OPEN; this indicates that the connection is ready to send and receive data.

Once you've opened your connection, you can begin transmitting data to the server.

window.onload = function() {
  var service = new WebSocket("wss://echo.websocket.org");
  service.onmessage = function(event) {
    alert("onmessage event: "+event.data);
  }
  service.onopen = function() {
    service.send("test"); //Will work here!
    //^^^^^^^^^^^^^^^^^
    service.send("hello!");
  }
  service.onclose = function() {
    alert("closed");
  }
  service.onerror = function() {
    alert("error");
  }

  //Can't close while a connection is still being established.
  //service.close();
}
like image 62
Rayon Avatar answered Oct 21 '22 11:10

Rayon


If you use Websocket().send() outside your service.onopen(), you can still check the readyStatebefore sending message, like this:

    websocket.sendmessage = async function(msg) {
       while (this.readyState === 0) {
            await sleep(200);
       }
       this.send(msg);
    };
like image 1
Antoine Draune Avatar answered Oct 21 '22 11:10

Antoine Draune