Skip to content

Commit

Permalink
Refactor Hummingbird.WebSocket to be inheritable
Browse files Browse the repository at this point in the history
* Two Hummingbird.WebSocket classes: Main and Weekly
* Reuse connection retrying
* Reuse WebSocket URI generator
  • Loading branch information
bdotdub committed May 11, 2010
1 parent 751a7cb commit 1aa6e99
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 66 deletions.
132 changes: 104 additions & 28 deletions public/js/websocket.js
@@ -1,12 +1,71 @@
if(!Hummingbird) { var Hummingbird = {}; }

Hummingbird.WebSocket = {};
Hummingbird.WebSocket.state = "stopped";
Hummingbird.WebSocket.start = function() {
if(!("WebSocket" in window)) {
console.log("Sorry, the build of your browser does not support WebSockets. Please try latest Chrome or Webkit nightly");
return;
Hummingbird.WebSocket = function() {
this.state = "stopped";
};

Hummingbird.WebSocket.prototype = {
// WebSocket callbacks
onclose: function() {
var self = this;
if(this.getState() == "retrying") {
// Wait a while to try restarting
console.log("still no socket, retrying in 3 seconds");
setTimeout(function() { self.start() }, 3000);
} else {
// First attempt at restarting, try immediately
this.setState("retrying");
console.log("socket lost, retrying immediately");
setTimeout(function() { self.start() }, 200);
}
},

onopen: function() {
this.setState("started");
console.log("socket started");
},

// Hummingbird WebSocket functions
getState: function() {
return this.state;
},

setState: function(state) {
this.state = state;
},

start: function() {
console.log("start() not implemented");
},

webSocketEnabled: function() {
if(!("WebSocket" in window)) {
console.log("Sorry, the build of your browser does not support WebSockets. Please try latest Chrome or Webkit nightly");
return false;
}
return true;
},

webSocketURI: function() {
if(document.location.search.match(/ws_server/)) {
var wsServerParam = document.location.search.match(/ws_server=([^\&\#]+)/) || [];
var wsPortParam = document.location.search.match(/ws_port=([^\&\#]+)/) || [];
var wsServer = "ws://" + wsServerParam[1] + ":" + (wsPortParam[1] || 8080);
} else {
var wsServer = "ws://" + document.location.hostname + ":8080";
}
return wsServer;
}
}

// MAIN WEBSOCKET

Hummingbird.WebSocket.Main = function() { }

This comment has been minimized.

Copy link
@bdotdub

bdotdub May 11, 2010

Author Collaborator

Should we rename this to Dashboard (from Main?)

This comment has been minimized.

Copy link
@mnutt

mnutt May 11, 2010

Owner

Yes, let's.

Hummingbird.WebSocket.Main.prototype = new Hummingbird.WebSocket;

Hummingbird.WebSocket.Main.prototype.start = function() {
if (!this.webSocketEnabled())
return;

var totalDiv = $("#log");
totalDiv.find('canvas').get(0).width = $(window).width() - 160;
Expand All @@ -16,14 +75,11 @@ Hummingbird.WebSocket.start = function() {
cartAdds.find('canvas').get(0).width = $(window).width() - 160;
var cartAddsGraph = new Hummingbird.Graph(cartAdds, { ratePerSecond: 20 });

if(document.location.search.match(/ws_server/)) {
var wsServerParam = document.location.search.match(/ws_server=([^\&\#]+)/) || [];
var wsPortParam = document.location.search.match(/ws_port=([^\&\#]+)/) || [];
var wsServer = "ws://" + wsServerParam[1] + ":" + (wsPortParam[1] || 8080);
} else {
var wsServer = "ws://" + document.location.hostname + ":8080";
}
var wsServer = this.webSocketURI();
var ws = new WebSocket(wsServer);

var self = this;

ws.onmessage = function(evt) {
var data = JSON.parse(evt.data);

Expand All @@ -44,20 +100,40 @@ Hummingbird.WebSocket.start = function() {
}
}
}
ws.onclose = function() {
if(Hummingbird.WebSocket.state == "retrying") {
// Wait a while to try restarting
console.log("still no socket, retrying in 3 seconds");
setTimeout(Hummingbird.WebSocket.start, 3000);
} else {
// First attempt at restarting, try immediately
Hummingbird.WebSocket.state = "retrying";
console.log("socket lost, retrying immediately");
setTimeout(Hummingbird.WebSocket.start, 200);

ws.onclose = function() { self.onclose(); }
ws.onopen = function() { self.onopen(); }
}

// WEEKLY WEBSOCKET

Hummingbird.WebSocket.Weekly = function() { }
Hummingbird.WebSocket.Weekly.prototype = new Hummingbird.WebSocket;

Hummingbird.WebSocket.Weekly.prototype.start = function() {
if (!this.webSocketEnabled())
return;

var wsServer = this.webSocketURI();
var ws = new WebSocket(wsServer);

var self = this;

ws.onmessage = function(evt) {
var data = JSON.parse(evt.data);
if(data.total && data.total > 0) {
var el = $("div.day:first-child div.all_views");
var prevTotal = el.data("total");
el.text((prevTotal + data.total).commify()).data('total', prevTotal + data.total);
}
if(data.cartAdds && data.cartAdds > 0) {
var el = $("div.day:first-child div.cart_adds");
var prevCartAdds = el.data("cart_adds");
el.text((prevCartAdds + data.cartAdds).commify()).data('cart_adds', prevCartAdds + data.cartAdds);
}
};
ws.onopen = function() {
Hummingbird.WebSocket.state = "started";
console.log("socket started");
};
};

ws.onclose = function() { self.onclose(); }
ws.onopen = function() { self.onopen(); }
}

34 changes: 0 additions & 34 deletions public/js/weekly.js
Expand Up @@ -62,39 +62,5 @@ Hummingbird.Weekly.init = function() {
});
});
});

if(document.location.search.match(/use_prod/)) {
var wsServerParam = document.location.search.match(/ws_server=([^\&\#]+)/) || [];
var wsPortParam = document.location.search.match(/ws_port=([^\&\#]+)/) || [];
var wsServer = "ws://" + wsServerParam[1] + ":" + (wsPortParam[1] || 8080);
} else {
var wsServer = "ws://" + document.location.hostname + ":8080";
}
var ws = new WebSocket(wsServer);
ws.onmessage = function(evt) {
var data = JSON.parse(evt.data);
if(data.total && data.total > 0) {
var el = $("div.day:first-child div.all_views");
var prevTotal = el.data("total");
el.text((prevTotal + data.total).commify()).data('total', prevTotal + data.total);
}
if(data.cartAdds && data.cartAdds > 0) {
var el = $("div.day:first-child div.cart_adds");
var prevCartAdds = el.data("cart_adds");
el.text((prevCartAdds + data.cartAdds).commify()).data('cart_adds', prevCartAdds + data.cartAdds);
}
};
ws.onclose = function() {
if(Hummingbird.WebSocket.state == "retrying") {
// Wait a while to try restarting
console.log("still no socket, retrying in 3 seconds");
setTimeout(Hummingbird.WebSocket.start, 3000);
} else {
// First attempt at restarting, try immediately
Hummingbird.WebSocket.state = "retrying";
console.log("socket lost, retrying immediately");
setTimeout(Hummingbird.WebSocket.start, 200);
}
};
};

4 changes: 3 additions & 1 deletion views/index.html.ejs
Expand Up @@ -30,7 +30,9 @@

<script>
$(document).ready(function() {
Hummingbird.WebSocket.start();
var hummingSocket = new Hummingbird.WebSocket.Main();
hummingSocket.start();
Hummingbird.getSales();
setInterval(Hummingbird.resortSales, 3000);
Expand Down
6 changes: 3 additions & 3 deletions views/weekly.html.ejs
Expand Up @@ -30,10 +30,10 @@
</div>

<script>
Hummingbird.WebSocket = {};
Hummingbird.WebSocket.state = "stopped";
$(function() {
var hummingSocket = new Hummingbird.WebSocket.Weekly();
hummingSocket.start();
Hummingbird.Weekly.init();
});
Expand Down

0 comments on commit 1aa6e99

Please sign in to comment.