Skip to content

Commit

Permalink
added buffer to sub, but it ain't working
Browse files Browse the repository at this point in the history
  • Loading branch information
joncon committed Jan 17, 2015
1 parent cdef8c8 commit 2ff1955
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
33 changes: 31 additions & 2 deletions app.js
Expand Up @@ -9,6 +9,8 @@ app.use(express.static(__dirname + '/public'));
var server = http.createServer(app);
var clientport = process.env.PORT || 8080;
server.listen(clientport);
var buffMax = 1000;
var buffer =[];

console.log('Started serving on port: ' + clientport);

Expand All @@ -28,7 +30,10 @@ wsDst.broadcast = function(data) { // broadcast data to all connnections
};

wsDst.on('connection', function(ws) { // on connecting

//start new connections with a full buffer
for(var i=0; i < buffer.length; i++){
wsDst.send(buffer[i]);
}
ws.id = connectionIDCounter; // set ID to counter
ws.IP = ws._socket.remoteAddress + ':' + ws._socket.remotePort;
allSocks[connectionIDCounter] = ws; // store socket in array object
Expand Down Expand Up @@ -61,6 +66,8 @@ function wsStart(){ // put the source websocket logic in a function for easy re
//var message = new Buffer(data).toString('base64');
//wsDst.broadcast(message);
wsDst.broadcast(data);
updateBuffer(data);

});

wsSrc.on('close', function(ws) {
Expand Down Expand Up @@ -104,4 +111,26 @@ Object.size = function(obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
};

//simple buffer treated as queue.
//this queue shift is really O(n) but
//since this is such a small array it shouldn't matter
function updateBuffer(msg){
var packetFound=false;
//check for dupes. Only need to look at the tail end of buffer
for(var i=buffer.length -6; i< buffer.length; i++){
if(msg == buffer[i]){
packetFound=true;
break;
}
}
if(!packetFound){
buffer.push(msg);
}
while(buffer.length > buffMax ){
buffer.shift();
}

// console.log(buffer);
}
2 changes: 1 addition & 1 deletion config.js
@@ -1,3 +1,3 @@
module.exports = {
sourceSocket: "ws://qspub.cloudapp.net:9999" // The WebSocket running EarthWorm/Mongoose/WebSWave
sourceSocket: "ws://qpub.trafficmanager.net:9999" // The WebSocket running EarthWorm/Mongoose/WebSWave
};

0 comments on commit 2ff1955

Please sign in to comment.