Skip to content

Instantly share code, notes, and snippets.

@railsstudent
Created January 11, 2017 12:31
Show Gist options
  • Save railsstudent/1a0d35ac2215ec06f02b2ddd4dbf52b6 to your computer and use it in GitHub Desktop.
Save railsstudent/1a0d35ac2215ec06f02b2ddd4dbf52b6 to your computer and use it in GitHub Desktop.
function NetworkClient (sendFunction, callback) {
this.sendFunction = sendFunction;
this.callback = callback;
this.msgIdAccumulator = [];
this.msgId = 0;
}
NetworkClient.prototype.send = function (data) {
// Could wrap data with extra information to send
this.msgId += 1;
this.sendFunction(data + this.msgId);
};
NetworkClient.prototype.recv = function (data) {
// Could unpack data and validate
var msgId = data.substring(data.length - 1);
var data = data.substring(0, data.length -1);
if (this.msgIdAccumulator.indexOf(msgId) < 0) {
this.msgIdAccumulator.push(msgId);
this.callback(data);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment