Skip to content

Instantly share code, notes, and snippets.

@DanBUK
Last active December 14, 2015 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanBUK/5081058 to your computer and use it in GitHub Desktop.
Save DanBUK/5081058 to your computer and use it in GitHub Desktop.
TCP Hello TCP Daemon
#!/bin/env node
var NET = require('net');
var ClientHandler = function (conn) {
this.bkspace = new Buffer([8,8,8]);
this._conn = conn;
this._running = true;
this._remoteAddress = this._conn.remoteAddress;
this._remotePort = this._conn.remotePort;
this._conn.on('end', this.disconnect.bind(this));
this._last = '\\o/';
this._next = '|o|';
console.log(this._remoteAddress + ':' + this._remotePort + ' Connected.');
this._intervalId = setInterval(this.wave_at.bind(this), 150);
};
ClientHandler.prototype.wave_at = function () {
if (this._running === true) {
this._conn.write(this.bkspace);
this._conn.write(this._next);
var tmp = this._last;
this._last = this._next;
this._next = tmp;
}
};
ClientHandler.prototype.disconnect = function () {
this._running = false;
clearInterval(this._intervalId);
console.log(this._remoteAddress + ':' + this._remotePort + ' Disconnected.');
};
var srv = NET.createServer(function (client) {
var c = new ClientHandler(client);
});
srv.listen(1338, function () {
console.log('Listening *:1338');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment