lib/jsonrpc-tcp/connection.js
function Connection(socket) {
  var self = this;
  
  events.EventEmitter.call(this);
  this._methods = {};
  this._socket = socket || new net.Socket();
  this._socket.setEncoding('utf8');
  this._socket.addListener('connect', function() {
    var remote = new Remote(self);
    self.emit('connect', remote);
  });
  this._socket.addListener('data', function(data) {
    //console.log('RECV: ' + data);
    self._parser.parse(data);
  });
  this._socket.addListener('end', this.emit.bind(this, 'end'));
  this._socket.addListener('timeout', this.emit.bind(this, 'timeout'));
  this._socket.addListener('drain', this.emit.bind(this, 'drain'));
  this._socket.addListener('error', this.emit.bind(this, 'error'));
  this._socket.addListener('close', this.emit.bind(this, 'close'));
  
  this._parser = new jsonsp.Parser(function(obj) {
    if (obj.result !== undefined || obj.error !== undefined) {
      self.emit('response', obj);
    } else if (obj.id !== null) {
      self.emit('request', obj);
      self._handleRequest(obj);
    } else {
      self.emit('notification', obj);
      self._handleRequest(obj);
    }
  });
  this._parser.addListener('error', function(err) {
    self._socket.destroy();
    self.emit('error', err);
  })
}
function Connection(socket) {
  var self = this;
  
  events.EventEmitter.call(this);
  this._methods = {};
  this._socket = socket || new net.Socket();
  this._socket.setEncoding('utf8');
  this._socket.addListener('connect', function() {
    var remote = new Remote(self);
    self.emit('connect', remote);
  });
  this._socket.addListener('data', function(data) {
    //console.log('RECV: ' + data);
    self._parser.parse(data);
  });
  this._socket.addListener('end', this.emit.bind(this, 'end'));
  this._socket.addListener('timeout', this.emit.bind(this, 'timeout'));
  this._socket.addListener('drain', this.emit.bind(this, 'drain'));
  this._socket.addListener('error', this.emit.bind(this, 'error'));
  this._socket.addListener('close', this.emit.bind(this, 'close'));
  
  this._parser = new jsonsp.Parser(function(obj) {
    if (obj.result !== undefined || obj.error !== undefined) {
      self.emit('response', obj);
    } else if (obj.id !== null) {
      self.emit('request', obj);
      self._handleRequest(obj);
    } else {
      self.emit('notification', obj);
      self._handleRequest(obj);
    }
  });
  this._parser.addListener('error', function(err) {
    self._socket.destroy();
    self.emit('error', err);
  })
}
function Connection ( socket ) { var self = this ; events . EventEmitter . call ( this ) ; this . _methods = { } ; this . _socket = socket || new net . Socket ( ) ; this . _socket . setEncoding ( 'utf8' ) ; this . _socket . addListener ( 'connect' , function ( ) { var remote = new Remote ( self ) ; self . emit ( 'connect' , remote ) ; } ) ; this . _socket . addListener ( 'data' , function ( data ) { //console.log('RECV: ' + data); self . _parser . parse ( data ) ; } ) ; this . _socket . addListener ( 'end' , this . emit . bind ( this , 'end' ) ) ; this . _socket . addListener ( 'timeout' , this . emit . bind ( this , 'timeout' ) ) ; this . _socket . addListener ( 'drain' , this . emit . bind ( this , 'drain' ) ) ; this . _socket . addListener ( 'error' , this . emit . bind ( this , 'error' ) ) ; this . _socket . addListener ( 'close' , this . emit . bind ( this , 'close' ) ) ; this . _parser = new jsonsp . Parser ( function ( obj ) { if ( obj . result !== undefined || obj . error !== undefined ) { self . emit ( 'response' , obj ) ; } else if ( obj . id !== null ) { self . emit ( 'request' , obj ) ; self . _handleRequest ( obj ) ; } else { self . emit ( 'notification' , obj ) ; self . _handleRequest ( obj ) ; } } ) ; this . _parser . addListener ( 'error' , function ( err ) { self . _socket . destroy ( ) ; self . emit ( 'error' , err ) ; } ) }
Create a new JSON-RPC connection.

Creates a new JSON-RPC over TCP connection.  The optional `socket` argument
is used to create a connection on an existing socket, otherwise a new socket
will be allocated.

Events:

Event: 'connect'

`function(remote) { }`

Emitted when a connection is established to a server.  `remote` is a
`Remote`, to be used for invoking remote methods on the server.

Event: 'request'

`function(req) { }`

Emitted when a request (method invocation) is received on the connection.

Event: 'response'

`function(res) { }`

Emitted when a response (to a method invocation) is received on the
connection.

Event: 'notification'

`function(notif) { }`

Emitted when a notification is received on the connection.

Event: 'error'

`function(err) { }`

Emitted when an error occurs.

Examples:

var connection = new Connection();

@return {Connection}
@api public
Create a new JSON - RPC connection .