dazeus.js
function (options, onConnect) {
    var self = this;
    EventEmitter.call(this);

    this.options = options;

    // debugging
    this.debug_enabled = options.debug;
    this.handshook = false;

    // received data which could not be parsed into messages yet
    this.data = '';

    // determine correct call of net.connect
    var cb = function () {
        connected.call(self);
        onConnect.call(self);
    };

    if (options.path) {
        this.debug("Trying to establish connection to unix socket %s", options.path);
        this.client = net.connect(options.path, cb);
    } else {
        this.debug("Trying to establish connection to %s on port %s", options.host, options.port);
        this.client = net.connect(options.port, options.host, cb);
    }

    // when data is received
    this.client.on('data', function (data) {
        var obj = dezeusify.call(self, data.toString('utf8'));
        obj.forEach(function (item) {
            received.call(self, item);
        });
    });

    // when the connection is closed
    this.client.on('end', function () {
        disconnected.call(self);
    });

    this.client.on('error', function (err) {
        self.debug("Whoops, an error occurred: %s", err.message);
        throw new Error(util.format("A connection error occurred: %s", err.message));
    });

    // when a new listener is added to this object, we'll want to check if we should notify the server
    this.on('newListener', function (evt) {
        this.debug("A new event listener was added");
        if (evt.toUpperCase() === evt && !this.subscribedEvents.indexOf(evt) !== -1) {
            subscribeServerEvent.call(self, evt);
        }
    });

    this.subscribedEvents = [];
    this.waitingCallbacks = [];
}
function (options, onConnect) {
    var self = this;
    EventEmitter.call(this);

    this.options = options;

    // debugging
    this.debug_enabled = options.debug;
    this.handshook = false;

    // received data which could not be parsed into messages yet
    this.data = '';

    // determine correct call of net.connect
    var cb = function () {
        connected.call(self);
        onConnect.call(self);
    };

    if (options.path) {
        this.debug("Trying to establish connection to unix socket %s", options.path);
        this.client = net.connect(options.path, cb);
    } else {
        this.debug("Trying to establish connection to %s on port %s", options.host, options.port);
        this.client = net.connect(options.port, options.host, cb);
    }

    // when data is received
    this.client.on('data', function (data) {
        var obj = dezeusify.call(self, data.toString('utf8'));
        obj.forEach(function (item) {
            received.call(self, item);
        });
    });

    // when the connection is closed
    this.client.on('end', function () {
        disconnected.call(self);
    });

    this.client.on('error', function (err) {
        self.debug("Whoops, an error occurred: %s", err.message);
        throw new Error(util.format("A connection error occurred: %s", err.message));
    });

    // when a new listener is added to this object, we'll want to check if we should notify the server
    this.on('newListener', function (evt) {
        this.debug("A new event listener was added");
        if (evt.toUpperCase() === evt && !this.subscribedEvents.indexOf(evt) !== -1) {
            subscribeServerEvent.call(self, evt);
        }
    });

    this.subscribedEvents = [];
    this.waitingCallbacks = [];
}
function ( options , onConnect ) { var self = this ; EventEmitter . call ( this ) ; this . options = options ; // debugging this . debug_enabled = options . debug ; this . handshook = false ; // received data which could not be parsed into messages yet this . data = '' ; // determine correct call of net.connect var cb = function ( ) { connected . call ( self ) ; onConnect . call ( self ) ; } ; if ( options . path ) { this . debug ( "Trying to establish connection to unix socket %s" , options . path ) ; this . client = net . connect ( options . path , cb ) ; } else { this . debug ( "Trying to establish connection to %s on port %s" , options . host , options . port ) ; this . client = net . connect ( options . port , options . host , cb ) ; } // when data is received this . client . on ( 'data' , function ( data ) { var obj = dezeusify . call ( self , data . toString ( 'utf8' ) ) ; obj . forEach ( function ( item ) { received . call ( self , item ) ; } ) ; } ) ; // when the connection is closed this . client . on ( 'end' , function ( ) { disconnected . call ( self ) ; } ) ; this . client . on ( 'error' , function ( err ) { self . debug ( "Whoops, an error occurred: %s" , err . message ) ; throw new Error ( util . format ( "A connection error occurred: %s" , err . message ) ) ; } ) ; // when a new listener is added to this object, we'll want to check if we should notify the server this . on ( 'newListener' , function ( evt ) { this . debug ( "A new event listener was added" ) ; if ( evt . toUpperCase ( ) === evt && ! this . subscribedEvents . indexOf ( evt ) !== - 1 ) { subscribeServerEvent . call ( self , evt ) ; } } ) ; this . subscribedEvents = [ ] ; this . waitingCallbacks = [ ] ; }
DaZeus client connection object
@param {Object} options
@param {Function} onConnect Function to be executed when the connection is established
DaZeus client connection object