Skip to content

Instantly share code, notes, and snippets.

@reel
Created July 8, 2014 13:15
Show Gist options
  • Save reel/a8351ee1135c47278b1b to your computer and use it in GitHub Desktop.
Save reel/a8351ee1135c47278b1b to your computer and use it in GitHub Desktop.
function SomeObject() {
this.socket = undefined;
this.description = undefined;
this.connected = undefined;
this.connect = function (ip) {
if (_.isUndefined(this.socket)) {
this.socket = net.connect("8787", ip);
if (!_.isUndefined(this.socket)) {
var that = this;
this.socket.on('connect', function () {
console.log("We are connected!");
this.connected = true;
});
this.socket.on('data', function (data) {
console.log("Received: " + that.decode(data.toString('utf-8')));
});
}
} else {
console.log("Already connected on " + ip);
}
};
this.disconnect = function () {
console.log("Read: " + this.socket.bytesRead);
console.log("Written: " + this.socket.bytesWritten);
this.socket.destroy();
};
this.send = function (command) {
//if (!this.connected) { return "Not connected"; }
var prepare = this.encode(command);
console.log("prepared: ", prepare);
this.socket.write(prepare);
};
this.encode = function (input) {
return input += "somehash";
};
this.decode = function (input) {
return input = somehash + "input";
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment