Reputation: 2061
im trying to use websockets for my application
function Connector()
{
this.protocol = "bla";
this.socket = new WebSocket("ws://echo.websocket.org", this.protocol);
}
Connector.prototype.emit = function()
{
this.socket.send('abc');
}
For me it is possible to call:
var con = new Connector();
con.emit();
from my Browsers console but from source code i get the following error message
InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable
what is the problem ?
Upvotes: 0
Views: 3562
Reputation: 2968
Are you waiting for the .onopen
function to be called ? you are supposed to send message on a websocket only after the connection is established. you can try something like
function Connector() {
this.protocol = "bla";
this.socket = new WebSocket("ws://echo.websocket.org", this.protocol);
this.connected = false;
}
Connector.prototype.emit = function() {
if(this.connected) {
try {
this.socket.send('abc');
return true;
}
catch(err) {
console.log('Error' + err.message);
return false;
}
}
else {
console.log('CRITICAL Error No WS');
return false;
}
}
con.socket.onopen = function() {
console.log("webSocket Connected");
con.connected = true;
//send message here
con.emit();
};
con.socket.onclose = function(e) {
//closed
con.connected = false;
};
con.socket.onerror = function(e) {
console.log("Error",e);
};
con.socket.onmessage = function(msg) {
//msg
}
UPDATED CODE
function Connector(protocol, cB){
var self = this,
this.protocol = protocol,
this.socket = new WebSocket("ws://echo.websocket.org", this.protocol),
this.connected = false;
this.socket.onopen = function() {
self.connected = true;
cB();
};
this.socket.onclose = function(e) {
self.connected = false;
//closed, inform(event)
};
this.socket.onerror = function(e) {
//error, inform(event)
};
this.socket.onmessage = function(msg) {
//msg, inform(event)
}
}
Connector.prototype.emit = function(msg) {
if(this.connected) {
try {
this.socket.send(msg);
return true;
}
catch(err) {
console.log('Error' + err.message);
return false;
}
}
else {
console.log('CRITICAL Error No WS');
return false;
}
}
var con = new Connector('blah',function() {
con.emit('abc');
});
and the simplest case with out a constructor or any other events; Just simple/basic ws client
var socket = new WebSocket("ws://echo.websocket.org", 'bla');
socket.onopen = function() {
socket.send('msg');
};
Upvotes: 2
Reputation: 1318
Shouldn 't You wait for the right value of readyState
?
function Connector(wsurl) {
this.protocol = "bla";
this.socket = new WebSocket(wsurl, this.protocol);
}
Connector.prototype.emit = function(msg) {
var self = this;
(function _waitForSocketConnection(callback) {
setTimeout(function() {
if (self.socket.readyState === 1) {
console.log("Connection is made")
if (callback != null) {
callback();
}
return;
} else {
console.log("wait for connection...")
_waitForSocketConnection(callback);
}
}, 5);
})(function() {
console.log("message sent!!!");
self.socket.send(msg);
});
}
var con = new Connector("ws://echo.websocket.org");
con.emit('abc');
//or even worst
new Connector("ws://echo.websocket.org").emit('def');
Upvotes: 3
Reputation: 785
You should wait for the socket to connect first. The Websocket constructor is non-blocking, which means that when you create a new Websocket, it connects in the background and your code continues. You need to set a onopen
attribute on your Websocket to a callback that will get executed when the Websocket connects.
Here you can see how it's done what events you can listen for.
Upvotes: 1