Permalink
Browse files

prevented from network send on non-connected states.

  • Loading branch information...
1 parent a222026 commit 1ba8e722e15a7f70ab6e356d118de1e9776ffde7 @stephenlb stephenlb committed Oct 2, 2012
Showing with 14 additions and 11 deletions.
  1. +13 −10 1110.js
  2. +1 −1 network.js
View
23 1110.js
@@ -290,39 +290,42 @@ var draw = function() {
drawAvatar(avatar, 0, 0, clientId);
};
-
var oldAvatar = {};
var mps = 0;
var mmps = 0;
var min_sample = 100;
-var ival = 0;
+var sample_send_ival = 0;
var sample_delivery = function () {
if (window.ws && ws.readyState) {
- var msg = {id:clientId};
+ var msg = {};
for(var key in avatar) {
if (key[0] !== "_" &&
(avatar[key] !== oldAvatar[key])) {
msg[key] = avatar[key];
}
}
- if (Object.keys(msg).length) {
- ws.send(msg);
+ if (Object.keys(msg).length && ws.readyState === ws.OPEN) {
+ avatar.id = clientId;
+ ws.send(avatar);
}
oldAvatar = $.extend({}, avatar)
}
}
+
+// Sample Rate Changing
function set_sample() {
- clearInterval(ival);
- ival = setInterval( sample_delivery, send_poll_interval );
+ clearInterval(sample_send_ival);
+ sample_send_ival = setInterval( sample_delivery, send_poll_interval );
}
set_sample();
// Vary Sample Rate
setInterval( function() {
mmps = mmps > mps ? mmps : mps;
- send_poll_interval = Math.ceil((mps||1)/2) * min_sample;
- send_poll_interval = send_poll_interval > 1000 ? 1000 : send_poll_interval;
+ //send_poll_interval = Math.ceil((mps||1)) * min_sample;
+ //send_poll_interval = send_poll_interval > 1000 ? 1000 : send_poll_interval;
//console.log(mps,mmps,'new_sample',send_poll_interval);
+ send_poll_interval = 500;
mps = 0;
set_sample();
}, 1000 );
@@ -352,7 +355,7 @@ var idle2 = function() {
if(connected) {
idleWarning = false;
ws.close();
- clearInterval(ival);
+ clearInterval(sample_send_ival);
}
};
var clearIdle = function() {
View
@@ -1,7 +1,7 @@
var ws, clientId = PUBNUB.uuid().split('-')[0], allAvatars={}, connected, netDebug = 5;
// hackernews mitigation by PUBNUB:
-serverURI = "ws://pubsub.pubnub.com/09e02e4d-5508-44fb-a615-8da69289b73c/a71204b3-ca89-11df-ba32-cfcef4a2b967/xkcd2"
+serverURI = "ws://pubsub.pubnub.com/09e02e4d-5508-44fb-a615-8da69289b73c/a71204b3-ca89-11df-ba32-cfcef4a2b967/xkdc2"
function rnd(n) { return Math.random() * n }
function r(n) { return rnd(n) - rnd(n) }

0 comments on commit 1ba8e72

Please sign in to comment.