Created
November 1, 2016 13:01
-
-
Save erayarslan/e142541208af21f1ba0cd4d2427ce45c to your computer and use it in GitHub Desktop.
node.js tcp example
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
| var Client = function (port, host, next) { | |
| this._counter = 0; | |
| this._reply = {}; | |
| this._chunk = ""; | |
| this._d_index = -1; | |
| this._interval = 5000; | |
| this._firstDrop = false; | |
| this._port = port; | |
| this._host = host; | |
| this._reconnector = undefined; | |
| this._ready = false; | |
| this._onCache = {}; | |
| this._socket = require('net').connect(this._port, this._host, (function () { | |
| this._ready = true; | |
| next ? next(this) : void 0; | |
| }).bind(this)); | |
| this._socket.on("connect", (function () { | |
| if (this._firstDrop) { | |
| clearInterval(this._reconnector); | |
| this._reconnector = undefined; | |
| } | |
| }).bind(this)); | |
| this._socket.on("end", this._drop.bind(this)); | |
| this._socket.on("error", this._drop.bind(this)); | |
| this._socket.on("close", this._drop.bind(this)); | |
| this._socket.on("data", (function (data) { | |
| this._parser(data); | |
| }).bind(this)); | |
| }; | |
| Client.prototype._reconnect = function () { | |
| if (!this._reconnector) { | |
| this._reconnector = setInterval((function () { | |
| this._socket.connect(this._port, this._host); | |
| }).bind(this), this._interval); | |
| } | |
| }; | |
| Client.prototype._parser = function (data) { | |
| this._chunk += data.toString(); | |
| this._d_index = this._chunk.indexOf(this._splitter); | |
| while (this._d_index > -1) { | |
| var string = this._chunk.substring(0, this._d_index); | |
| this._chunk = this._chunk.substring(this._d_index + 1); | |
| this._d_index = this._chunk.indexOf(this._splitter); | |
| try { | |
| var json = JSON.parse(string); | |
| if (this._reply.hasOwnProperty(json.reply_id)) { | |
| var temp = json.reply_id; | |
| delete json.reply_id; | |
| this._reply[temp](null, json); | |
| delete this._reply[temp]; | |
| } else { | |
| if (this._onCache.hasOwnProperty("data")) { | |
| this._onCache["data"](json); | |
| } | |
| } | |
| } catch (ex) { | |
| // skip | |
| } | |
| } | |
| }; | |
| Client.prototype._drop = function () { | |
| this._socket.end(); | |
| this._firstDrop = true; | |
| this._reconnect(); | |
| }; | |
| Client.prototype._splitter = ''; | |
| Client.prototype._toWritable = function (data) { | |
| return JSON.stringify(data) + this._splitter; | |
| }; | |
| Client.prototype._send = function (data) { | |
| if (this._ready && this._socket.writable) { | |
| this._socket.write(this._toWritable(data)); | |
| } | |
| }; | |
| Client.prototype.write = function (obj, next) { | |
| if (!!next) { | |
| var id = this._counter++ + "_" + new Date().getTime(); | |
| obj.reply_id = id; | |
| this._reply[id] = next; | |
| setTimeout((function () { | |
| if (this._reply.hasOwnProperty(id)) { | |
| this._reply[id](true, null); | |
| delete this._reply[id]; | |
| } | |
| }).bind(this), this._interval); | |
| } | |
| this._send(obj); | |
| }; | |
| Client.prototype.on = function (key, f) { | |
| this._onCache[key] = f; | |
| }; | |
| module.exports = Client; |
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
| var server = require("./server"); | |
| var client = require("./client"); | |
| server(1337, function (server) { | |
| server.on("data", function (client, data) { | |
| server.write(client, data.answer({"ho": "ohoohoho"})); | |
| }); | |
| }); | |
| client(1337, "localhost", function (server) { | |
| server.write({"test": 5}, function (err, data) { | |
| console.log(data); | |
| }); | |
| }); |
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
| var Server = function (port, next) { | |
| this._chunk = ""; | |
| this._d_index = -1; | |
| this._onCache = {}; | |
| this._sockets = {}; | |
| this._ready = false; | |
| /** | |
| * @param socket.remoteAddress socket | |
| * @param socket.remotePort socket | |
| */ | |
| require("net").createServer((function (socket) { | |
| socket.id = socket.remoteAddress + "." + socket.remotePort; | |
| socket.on("data", (function (data) { | |
| if (this._onCache.hasOwnProperty("data")) { | |
| this._parser(data, socket, this._onCache["data"]); | |
| } | |
| }).bind(this)); | |
| this._sockets[socket.id] = socket; | |
| }).bind(this)).listen(port, (function () { | |
| this._ready = true; | |
| next ? next(this) : void 0; | |
| }).bind(this)); | |
| }; | |
| Server.prototype._parser = function (data, from, next) { | |
| this._chunk += data.toString(); | |
| this._d_index = this._chunk.indexOf(this._splitter); | |
| while (this._d_index > -1) { | |
| var string = this._chunk.substring(0, this._d_index); | |
| this._chunk = this._chunk.substring(this._d_index + 1); | |
| this._d_index = this._chunk.indexOf(this._splitter); | |
| try { | |
| next(from, this._answerMode(JSON.parse(string))); | |
| } catch (ex) { | |
| // skip | |
| } | |
| } | |
| }; | |
| Server.prototype._splitter = ''; | |
| Server.prototype._toWritable = function (data) { | |
| return JSON.stringify(data) + this._splitter; | |
| }; | |
| Server.prototype.write = function (socket, data) { | |
| if (this._ready && socket.writable) { | |
| socket.write(this._toWritable(data)); | |
| } | |
| }; | |
| Server.prototype.on = function (key, f) { | |
| this._onCache[key] = f; | |
| }; | |
| Server.prototype._answerMode = function (obj) { | |
| obj.answer = function (data) { | |
| if (obj.hasOwnProperty("reply_id")) { | |
| data.reply_id = obj.reply_id; | |
| } | |
| return data; | |
| }; | |
| return obj; | |
| }; | |
| module.exports = Server; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment