Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 76 lines (58 sloc) 2.55 KB

A simple tcp server and client wrapper to enable line-by-line communication, communication of JSON objects, and resilence to server failure.


npm install


For the server:

// include this for all servers
TCPServer = require('simpletcp').server();

// include this is you are going to be passing json objects back and forth, otherwise use LineConnection
JSONConnection = require('simpletcp').JSONConnection();

// If you are doing anything more than simply echoing, you should
// define a class that will represent each new connection, e.g.,

function Connection(server, socket) {, server, socket);
    var me = this;
    this.on('data', function(obj) { me.onJSON(obj); });
    this.on('malformed', function(str) { console.log('bad json received: '+str); });
require('util').inherits(Connection, JSONConnection);

// called when a new connection has started
Connection.prototype.start = function()
    this.write({msg: 'feed me'});   // tell client we are alive

// do something with objects we get from the client
Connection.prototype.onJSON = function(obj)
    var a = obj.a || 0;
    var b = obj.b || 0;
    obj.answer = a+b;

// create and start the server on port 8080
var server = new TCPServer('adder', 8080, Connection);
server.on('listen', function() {  console.log("Server started"); });
server.on('connection', function(conn) { conn.start(); });
server.on('error', function(err) { console.log('Trying to start server Got error: '+err); server.destroy(); });

For the client:

var util=require('util');
var TCPClient = require('simpletcp').client();

// create a connection to localhost:8080
var client = new TCPClient('client1',
                           function() { 
                               // called when connection is established
                               client.writeJSON({a:1, b:3});
                               // we are done
client.on('data', function(obj) { console.log(obj); });
// open the connectin;