1.0.0 • Published 3 years ago

websocket-teste v1.0.0

Weekly downloads
32
License
MIT
Repository
github
Last release
3 years ago

WebSocket

A complete and minimalist WebSocket under the protocol RFC-6455 for Node.js server ≥ v14.x

Which is?

It is a socket type connection (real-time, bidirectional and persistent) where both parties (server and client) can sending data (Text, Blob or ArrayBuffer) at any time.

Interfaces

// Constructor
WebSocket(
    server: Object, // HTTP(s) Server Object
    {
        allowOrigin?: String | Array<String> | Null, // Allowed domains (Default: Null)
        pingDelay?: UInteger, // Delay between sending ping's (Default: 180000ms)
        encoding?: String, // [utf8 | ascii | base64 | hex | binary | utf16le | ucs2] (Default: utf8)
        limitByIP?: UInteger, // IP access limit (Default: 256)
        maxPayload?: UInteger, // Maximum size in bytes that a message can be (Default: 2621440)
        pongTimeout?: UInteger // Maximum pong waiting time (Default: 5000ms)
    }?
): Class
// Getters
allowOrigin(): String | Array<String> | Null

clients(): Array<String> // List of connected user ID's

encoding(): String

limitByIP(): UInteger

maxPayload(): UInteger

pongTimeout(): UInteger
// Setters
allowOrigin(input?: String | Array<String> | Null): Void // (Default: Null)

encoding(input?: String): Void // (Default: utf8)

limitByIP(input?: UInteger): Void // (Default: 256)

maxPayload(input?: UInteger): Void // (Default: 2621440 bytes)

pongTimeout(input?: UInteger): Void // (Default: 5000ms)
// Methods

/* Socket Methods Begin (https://nodejs.org/docs/latest/api/net.html#net_class_net_socket) */
    bytesRead(clientId: String): Boolean | Null

    bytesWritten(clientId: String): Boolean | Null

    isPaused(clientId: String): Boolean | Null

    pause(clientId: String): Boolean | Null

    readyState(clientId: String): Boolean | Null

    resume(clientId: String): Boolean | Null

    setEncoding(clientId: String, encoding?: String): Boolean | Null

    setKeepAlive(clientId: String, enable?: Boolean, initialDelay?: UInteger): Boolean | Null

    setNoDelay(clientId: String, noDelay?: Boolean): Boolean | Null
/* Socket Methods End */

close(clientId: String): Boolean | Null

ping(clientId: String, pongTimeout?: UInteger): Boolean | Null

send(
    clientId: String,
    data: String | Buffer, // Message content (if string, opcode 0x1, if not, 0x2)
    encoding?: String // [utf8 | ascii | base64 | hex | binary | utf16le | ucs2] (Default: utf8)
): Boolean | Null

How to use

// Front-end
const webSocket = new WebSocket((location.protocol === 'https:' ? 'wss://' : 'ws://') + location.host + '/path');

// webSocket.binaryType = 'blob';
// webSocket.binaryType = 'arraybuffer';

webSocket.onclose = e => console.log('Close', e);

webSocket.onerror = e => console.log('Error', e);

webSocket.onopen = () => {

    webSocket.send('Hello World');

    webSocket.onmessage = (e) => console.log('Message', e);

};
// Back-end
const WebSocket = require('@jadsonlucena/websocket');

let webSocket = new WebSocket(server);

webSocket.on('open', clientId => console.log('Connect', clientId));

webSocket.on('close', (clientId, e) => console.log('Close', clientId, e));

webSocket.on('error', (clientId, e) => console.log('Error', clientId, e));

// webSocket.on('message', (clientId, data) => {
webSocket.on('/path', (clientId, data) => {

    console.log('Data', clientId, data);

    // Single Client
    webSocket.send(clientId, data);

    // Broadcast
    webSocket.clients.forEach(id => webSocket.send(id, data));

});

If the /path is not specified in the WebSocket constructor on the front-end, the default listener on the back-end is "message"