lib/ipcInterface.js
function(options) {

	this.options = {
		"debug": false,
		"verbose": false,
		"socket": "/tmp/node-mpv.sock"
	}

	this.options = _.defaults(options || {}, this.options);

	// intialize the event emitter
	eventEmitter.call(this);

	// socket object
	this.socket = new net.Socket();

	// partially "fixes" the EventEmitter leak
	// the leaking listeners is "close", but I did not yet find any solution to fix it
	this.socket.setMaxListeners(0);

	// connect
	this.socket.connect({path: this.options.socket}, function() {
		if(this.options.debug){
			console.log("Connected to socket " + this.options.socket);
		}
	}.bind(this));


	// reestablish connection when lost
	this.socket.on('close', function() {
		if(this.options.debug){
			console.log("Lost connection to socket. Atemping to reconnect");
		}
		// properly close the connection
		this.socket.end();
		// reconnect
		this.socket.connect({path: this.options.socket}, function() {
			if(this.options.verbose || this.options.debug){
				console.log("Connected to socket " + this.options.socket);
			}
		}.bind(this));
	}.bind(this));

	//  catch errors when occurrings
	this.socket.on('error', function(error) {
		if(this.options.debug){
			console.log(error);
		}
	}.bind(this));

	// received data is delivered upwards by an event
	this.socket.on('data', function(data) {
		// various messages might be fetched at once
		var messages = data.toString().split('\n');

		// each message is emitted seperately
		messages.forEach(function (message) {
			// empty messages may occur
			if(message.length > 0){
				this.emit('message', JSON.parse(message));
			}
		}.bind(this));

	}.bind(this));

}
function(options) {

	this.options = {
		"debug": false,
		"verbose": false,
		"socket": "/tmp/node-mpv.sock"
	}

	this.options = _.defaults(options || {}, this.options);

	// intialize the event emitter
	eventEmitter.call(this);

	// socket object
	this.socket = new net.Socket();

	// partially "fixes" the EventEmitter leak
	// the leaking listeners is "close", but I did not yet find any solution to fix it
	this.socket.setMaxListeners(0);

	// connect
	this.socket.connect({path: this.options.socket}, function() {
		if(this.options.debug){
			console.log("Connected to socket " + this.options.socket);
		}
	}.bind(this));


	// reestablish connection when lost
	this.socket.on('close', function() {
		if(this.options.debug){
			console.log("Lost connection to socket. Atemping to reconnect");
		}
		// properly close the connection
		this.socket.end();
		// reconnect
		this.socket.connect({path: this.options.socket}, function() {
			if(this.options.verbose || this.options.debug){
				console.log("Connected to socket " + this.options.socket);
			}
		}.bind(this));
	}.bind(this));

	//  catch errors when occurrings
	this.socket.on('error', function(error) {
		if(this.options.debug){
			console.log(error);
		}
	}.bind(this));

	// received data is delivered upwards by an event
	this.socket.on('data', function(data) {
		// various messages might be fetched at once
		var messages = data.toString().split('\n');

		// each message is emitted seperately
		messages.forEach(function (message) {
			// empty messages may occur
			if(message.length > 0){
				this.emit('message', JSON.parse(message));
			}
		}.bind(this));

	}.bind(this));

}
function ( options ) { this . options = { "debug" : false , "verbose" : false , "socket" : "/tmp/node-mpv.sock" } this . options = _ . defaults ( options || { } , this . options ) ; // intialize the event emitter eventEmitter . call ( this ) ; // socket object this . socket = new net . Socket ( ) ; // partially "fixes" the EventEmitter leak // the leaking listeners is "close", but I did not yet find any solution to fix it this . socket . setMaxListeners ( 0 ) ; // connect this . socket . connect ( { path : this . options . socket } , function ( ) { if ( this . options . debug ) { console . log ( "Connected to socket " + this . options . socket ) ; } } . bind ( this ) ) ; // reestablish connection when lost this . socket . on ( 'close' , function ( ) { if ( this . options . debug ) { console . log ( "Lost connection to socket. Atemping to reconnect" ) ; } // properly close the connection this . socket . end ( ) ; // reconnect this . socket . connect ( { path : this . options . socket } , function ( ) { if ( this . options . verbose || this . options . debug ) { console . log ( "Connected to socket " + this . options . socket ) ; } } . bind ( this ) ) ; } . bind ( this ) ) ; //  catch errors when occurrings this . socket . on ( 'error' , function ( error ) { if ( this . options . debug ) { console . log ( error ) ; } } . bind ( this ) ) ; // received data is delivered upwards by an event this . socket . on ( 'data' , function ( data ) { // various messages might be fetched at once var messages = data . toString ( ) . split ( '\n' ) ; // each message is emitted seperately messages . forEach ( function ( message ) { // empty messages may occur if ( message . length > 0 ) { this . emit ( 'message' , JSON . parse ( message ) ) ; } } . bind ( this ) ) ; } . bind ( this ) ) ; }
connects to a socket reconnects when connection lost emits 'message' event when data is received from the socket
connects to a socket reconnects when connection lost emits message event when data is received from the socket