Last active
December 14, 2015 11:39
-
-
Save DanBUK/5081058 to your computer and use it in GitHub Desktop.
TCP Hello TCP Daemon
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
#!/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