Skip to content
This repository has been archived by the owner on Jun 18, 2020. It is now read-only.

weheartit/simple-socket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Simple Socket

This is a lightweight wrapper around the Node.js Socket class that makes reconnecting and handling errors super simple. It works with net and tls.

Installation

$ npm install simple-socket

Usage

var Socket = require('simple-socket').Socket;

var socket = new Socket({host: 'localhost', port: 80});

connect();

function connect() {
  socket.connect(connectHandler);

  function connectHandler(err) {
    if (err) {
      console.log('error connecting to server', err.stack);
      console.log('attempting to reconnect in 1 second');
      setTimeout(connect, 1000);
    }

    // reconnect when the socket is disconnected
    socket.onDisconnect = connect;

    // reconnect when there is a socket timeout
    socket.onTimeout = function() {
      // set onDisconnect to null before disconnect
      // if you don't want to reconnect
      // socket.onDisconnect = null;

      // disconnect clears all state and onDisconnect will
      // get called
      socket.disconnect();
    };

    // handle incoming data
    socket.onData = function(data) {
      // socket.writeable() will return false if the socket is 
      // disconnected or the underlying socket is not writable
      if (socket.writable()) {
        // socket.write() will callback with an error 
        // (close events before drain are treated as an error)
        socket.write(data, function(err) {
          if (err) console.error('unexepected error', err.stack);
        });
      }
    };

    // socket.pipe(destination) is also available
  };
};

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published