Noteworthy Programming Masterpiece

    socket-emitter
    TypeScript icon, indicating that this package has built-in type declarations

    1.5.6 • Public • Published

    Socket-Emitter

    A TCP Socket Tool, it make sending a message by tcp just like emitting an event.

    It can send most common type of data without any extra work.

    It's able to send large data.

    Data will be received after 50ms.

    Server Example:

    var Server = require('socket-emitter').Server;
     
    var PORT = 3766;
    var options = {
      // compress: 'zlib',
    }
     
    var server = new Server(options);
    server.on('connection', function(socket) {
      console.log('[Server]New Connection');
     
      socket.on('message', function(message) {
        console.log(message);
        // message will be 2
      });
     
      socket.emit('sendString', 'hello world');
     
      socket.emit('sendObject', {
        message: '123',
        count: 23
      });
     
      var buffer = new Buffer(4);
      buffer.writeUInt32BE(0x1234, 0);
      socket.emit('sendBuffer', buffer);
     
      socket.emit('endSend');
    });
    server.listen(PORT, function() {
      console.log('Server Started');
    });
     

    Client Example:

    var Client = require('socket-emitter').Client;
     
    var PORT = 3766;
    var options = {
      // compress: 'zlib',
    }
     
    var client = new Client(options);
    client.connect(PORT, 'localhost', function() {
      console.log('Client Connected');
      client.emit('message', 2);
    });
     
    client.on('sendString', function(message) {
      console.log(message);
      // message will be ‘hello world’
    });
     
    client.on('sendObject', function(message) {
      console.log('message');
      // message will be {message: '123', count: 23}
    });
     
    client.on('sendBuffer', function(message) {
      console.log(message);
      // message will be <Buffer 00 00 12 34>
    });
     
    client.on('endSend', function(message) {
      console.log(message);
      // message will be {}
      client.close(function() {
        console.log('Client Closed');
      });
    });

    Sample API


    SendableDataType

    Socket is able to send most of the data types, such as String, Number, Buffer, Object, Array, null, and others. If the data's type is Object, each of it's property value can be a SendableDataType data, such as :

    {
      str: 'abcdef',
      num: 23,
      buffer: new Buffer(10),
      array: [1, 'abc', /* and others*/],
      // and others
    }

    If the data's type is Array, each entry of the Array can be a SendableDataType data, such as :

    [
      'abc',
      {obj: true},
      new Buffer(20),
      [1, 2, 3],
      // and others
    ]

    new Server([options])

    Create a Server instance with options.

    options

    Type: Object

    [options.compress = null]

    If options.compress is equal to 'zlib', Socket will compress data before send.

    Sometimes it will be slower than sending data without compress.

    Server's Event

    connection

    Server will emit a connection event when there is a new connection.

    The only argument is a Socket, you can communicate with Client through it.

    error

    Server will emit an error event when there is an error.

    others

    Server will emit other event in different situations.

    Server.listen

    It's used to listen Client's connection, is same as net.Server.listen.

    Server.broadcast(event, arg[, except])

    Send data to all clients.

    event

    Type: String

    arg

    Type: SendableDataType

    except

    Type: Socket | Array of Socket

    Server will not send data to these Sockets.

    Server.sockets

    It an array of online Clients.

    Server.sockets.on(event ,listener)

    A method to receive data.

    event

    Type: String

    listener

    Type: Function(socket, arg)

    Function will be called when any Socket has received a data and event is same as sender's event argument. The first argument socket is the socket witch had received this data.


    new Client([options])

    Create a Client instance with options, options is same as above.

    Client is a subclass of Socket, the only extra method is connect.

    Client.connect

    It's used to connect to Server, is same as net.Socket.connect.


    Socket.emit(event[, arg])

    Method to send data.

    event

    Type: String

    arg

    Type: SendableDataType

    Socket.on(event, listener)

    Method to receive data.

    event

    Type: String

    listener

    Type: Function(arg)

    Function will be called when Socket has received a data and event is same as sender's event argument.


    More info to see at socket-emitter

    Install

    npm i socket-emitter

    DownloadsWeekly Downloads

    25

    Version

    1.5.6

    License

    BSD-2-Clause

    Last publish

    Collaborators

    • chenyu