Ping-pong plugin

For every received 'ping' string, this plugin will send an 'pong' string.

A simple heartbeat protocol on application level to keep an inactive WebSocket connection open.

Code

'use strict';

/**
 * @constructor
 * @struct
 */
function PingPongPlugin() {
    /**
     * @param {WebSocketMessage} message
     * @param {WebSocketConnection} connection
     */
    this.onMessage = (message, connection) => {
        if (message.data === 'ping') {
            connection.send('pong');
        }
    };
}

module.exports = {
    name: 'pingPong',
    description: 'For every received "ping" string, this plugin will send an "pong" string.',
    createWorker: () => new PingPongPlugin()
};