Server

Server

new Server(optionsopt)

Source:

Line Server Class

Example
const Server = require('line-socket/server');
const server = new Server({
  port: 8080
});
Parameters:
Name Type Attributes Description
options Object <optional>

Options object.

Properties
Name Type Attributes Description
host string <optional>

The hostname where to bind the server. Inherited from uws.

port number <optional>

The port where to bind the server. Inherited from uws.

server http.Server <optional>

A pre-created Node.js HTTP server. If provided, host and port will ignored. Inherited from uws.

path string <optional>

Accept only connections matching this path. Inherited from uws

responseTimeout number <optional>

Default timeout duration (in ms) for message responses. Default: 10000 (10 seconds)

handshakeTimeout number <optional>

This is the duration how long a client can stay connected without handshake. Default 60000 (1 minute).

pingInterval number <optional>

Ping interval in ms. Default: 15 seconds.

Extends

  • EventEmitterExtra

Members

(static, readonly) ErrorCode :string

Source:
Properties:
Name Type Description
INVALID_OPTIONS string

When constructing new Server(), this error could be thrown.

INVALID_ACTION string

This error can be seen in rejection of server.start() or server.stop() methods.

WEBSOCKET_ERROR string

This error is for native websocket errors.

Type:
  • string

(static, readonly) Event :string

Source:
Properties:
Name Type Description
HANDSHAKE string

handshake When a client connection is established, this event will be fired before connection event. Please note that, this event has nothing in common with native websocket handshaking process. If you want to authorize your clients, you must listen this event and call handshake.resolve(...) or handshake.reject(...) accordingly. If you do not consume this events, all the client connections will be accepted.

function (connection, handshake) {}

where connection is ServerConnection and handshake is a Message instance.

CONNECTION string

connection This event will fire on a client connects after successful handshake.

function (connection) {}

where connection is a ServerConnection instance.

HEADERS string

'headers' Inherited from uws, see docs

ERROR string

'error' Inherited from uws, see docs

Type:
  • string
Example
server.on('connection', (connection) => {
  connection.send('hello');
  ...
});

// or better, you can use enums

server.on(Server.Event.CONNECTION, (connection) => {
  connection.send('hello');
  ...
});

// If you want to authorize your client
server.on('handshake', (connection, handshake) => {
  if (handshake.payload && handshake.payload.authToken == '...')
    handshake.resolve({welcome: 'bro'});
  else
    handshake.reject(new Error('Invalid auth token'));
});

Methods

broadcast(name, payloadopt)

Source:

Broadcasts a message to all the connected (& handshaked) clients.

Example
server.broadcast('hello', {optional: 'payload'});
Parameters:
Name Type Attributes Description
name string

Message name

payload any <optional>

Optional message payload.

getConnectionById(id) → (nullable) {ServerConnection}

Source:

Gets a connection by id

Example
const connection = server.getConnectionById('someId');

if (connection) {
  connection.send('hello', {optional: 'payload'});
}
Parameters:
Name Type Description
id string

Unique connection id, which can be accessed at connection.id

Returns:
Type
ServerConnection

getConnections() → {Object}

Source:

Returns a object where keys are connection id and values are ServerConnection.

Returns:
Type
Object

getRoom(room) → (nullable) {ServerRoom}

Source:

Gets a room by name.

Parameters:
Name Type Description
room string

Room name

Returns:
Type
ServerRoom

getRoomsOf(connection) → {Array.<string>}

Source:

Gets all the rooms of a connection.

Parameters:
Name Type Description
connection ServerConnection
Returns:

Array of room names.

Type
Array.<string>

removeFromAllRooms(connection)

Source:

Remove a connection from all the rooms.

Parameters:
Name Type Description
connection ServerConnection

start() → {Promise}

Source:

Starts the server.

Example
server
  .start()
  .then(() => {
    console.log('Server started');
  })
  .catch((err) => {
    console.log('Server could not started', err);
  });
Returns:
Type
Promise

stop() → {Promise}

Source:

Stops the server.

Example
server
  .stop()
  .then(() => {
    console.log('Server stopped');
  })
  .catch((err) => {
    console.log('Server could not stopped', err);
  });
Returns:
Type
Promise