Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tag: v0.2.4
Fetching contributors…

Cannot retrieve contributors at this time

123 lines (95 sloc) 3.003 kB

NsSocket

NameSpace based Event based Buffered TCP/TLS Stream

Purpose

Wrap a network socket with automatic buffering to simply network string communication into events

Major Dependency

NsSocket Inherits from eventemitter2

Installation

npm install nssocket
or
git clone git@github.com:nodejitsu/nssocket.git

API

new nssocket.NsSocket(socket, config)

  • socket network (tcp or tls) socket
  • config config options for this NsSocket

NsSocket.send(dataArray)

  • dataArray Array object that will be sent, should not include delimiter

NsSocket.on(event, callback)

  • event name of the event
  • callback - the callback function Register callbacks to events on this NsSocket

NsSocket.emit(event, [data], ...)

  • event event to emit use at your own risk

NsSocket.connect(port, [host], [callback])

  • port destination port
  • host destination host, ip or hostname
  • callback callback on successful connect

NsSocket.end()

  • closes the current socket, emits close event, possibly also error

NsSocket.destroy()

  • remove all listeners, destroys socket, clears buffer
  • should normally use NsSocket.end()

Events

start

function () {}

Emitted once the underlying socket has connected/started

data

function (data) {} Emitted on raw data received on the underlying socket

data :: * :: ...

function (dataArray) {} Emitted once when a full message has been received on the socket, DataArray will be an Array object with items corresponding to the segments of the message

e.g. some::evented:blahbblahblah would be caught with

nsSocket.on('data::some::evented', function (datas) {
  console.log(datas);
}

error

function (err) {}

Emitted when there are any errors

close

function (had_err) {}

Emitted when the underlying connection is closed, had_err will be true if there were any errors.

idle

emitted when the socket has been idle, only emitted if setKeepAlive or setTimeout has been bound

Usage Demo

var net = require('net'),
    nssocket = require('nssocket');

// Config object
var config = {
  delimiter : '::' // default (recommended)
  type : 'tcp' // default (tcp, tls)
  msgLength : 3 // default (can be any length, technically even 1)
};

// socket gets wrapped
var socket = new net.Socket({ type: 'tcp4'}),
    nsSocket = nssocket.NsSocket(socket, config);

nsSocket.connect(80, '127.0.0.1', function onConnect() {
  // pass in an array
  nsSocket.send(['hello', 'world', 'derp']);
  console.dir('success!');  
});

Coming soon

  • More & Better Tests
  • Make Demo in examples/
  • Add automatic socket creation (no more passing in sockets!)
  • Add UDP support
  • Add nssocket.Server functionality (returns a nssocket!)
  • Cake(?)

Demo

TBA

Maintainers

Paolo Fragmenti, Jameson Lee

License

MIT?

Jump to Line
Something went wrong with that request. Please try again.