BrainBoxLabs / brain-socket-js

JS helper class for event-driven Websockets with Brainsocket & Laravel

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Uncaught InvalidStateError: Failed to execute 'send' on 'WebSocket'

BrunoQuaresma opened this issue · comments

I have this code

window.app = {};

        app.BrainSocket = new BrainSocket(
                new WebSocket('ws://localhost:8080'),
                new BrainSocketPubSub()
        );

        app.BrainSocket.message('server.some_event',{data:'some_data'});

        app.BrainSocket.Event.listen('client.some_event',function(msg){
            //data returned from laravel
            console.log(msg);
                //msg.server.data has the data "This event was fired from the server"
        });

And this error in chrome

Uncaught InvalidStateError: Failed to execute 'send' on 'WebSocket': already in CONNECTING state.

Just encountered the same issue, in firebug i am getting
"InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable"

I think it has something to do with the websockets connection not being open when it reaches the "app.BrainSocket.message" call. (for instance wrapping the call within a setTimeout of 500 ms seems to work (but is a dirty hack).

We would need to hook the native websockets 'onOpen' handler somewhere for this to work, off to further testing.

EDIT: Fixed in Pull request #4