Skip to content

Instantly share code, notes, and snippets.

@gmarland
Created June 10, 2013 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmarland/5751680 to your computer and use it in GitHub Desktop.
Save gmarland/5751680 to your computer and use it in GitHub Desktop.
Connecting to websocket using engine.io
connectSockets: function() {
var that = this;
if (this._socket != null) {
console.log("Retrying webbsocket connection");
this._socket.removeAllListeners();
this._socket = null;
}
this._socket = new eio("http://localhost:8080/");
// Handle socket actions
this._socket.onopen = function() {
console.log("Connected to websocket");
that._socket.onmessage = function(package){
var socketPackage = JSON.parse(package.data);
if (socketPackage.action != null) {
switch(socketPackage.action) {
case "topicUpdated":
break;
}
}
};
};
this._socket.onclose = function() {
console.log("Disconnected from websocket");
if (that._attemptReconnect) {
that._connectionAttempts++;
if (that._connectionAttempts < 10) {
that.connectSockets();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment