I am working with a WebSocket and trying to be able to send socket data at anytime from throughout my application. When I attempt to access the send command from within another function, I am receiving:
Uncaught InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable.
This only is occuring when I call a function, this is how I am setting up my websocket:
Main.socket = (function() {
var socket = new WebSocket("ws://server:port");
socket.onopen = function() {
console.log("Socket has been opened!");
}
function send() {
socket.send('test');
}
return {
socket: socket,
send: send
}
})();
I am able to call the function globally, and also when I console.log Main.socket from within a function it is able to see the socket. But when I call the send function I get that error.
onopenbeen called? "An attempt was made to use an [Socket] object that is not, or is no longer, usable [because it is in an Invalid State]."sendin asetTimeout(function () { Main.socket.send("yay") }, 500). Of course this is a silly amount of time to wait, in reality we should only wait untilonopenis triggered. Using futures can make this chaining easy, but it will require asynchronous usage in the consumer.