javascript - Websocket return from function -


im trying use websockets 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 possible call:

var con = new connector(); con.emit(); 

from browsers console source code following error message

invalidstateerror: attempt made use object not, or no longer, usable 

what problem ?

shouldn 't wait 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 made")                 if (callback != null) {                     callback();                 }                 return;              } else {                 console.log("wait 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 worst new connector("ws://echo.websocket.org").emit('def'); 

Comments