Created
January 11, 2017 12:31
-
-
Save railsstudent/1a0d35ac2215ec06f02b2ddd4dbf52b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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