Module: web-socket-server

Module for running a local web socket server. This server does not run as a service This is not a background service. When the cordova view is destroyed/terminated, the server is stopped. This module is based on the cordova-plugin-websocket-server plugin at https://github.com/becvert/cordova-plugin-websocket-server. This is a minimalistic plugin so read the warnings below.

WARNING: The plugin used for this module does not support any other network interface than 0.0.0.0 (binds to all network interfaces). But since it is sometimes desirable to block all connections that don't originate from localhost, I manually implemented the following policy on top of the plugin: If host interface is given as '127.0.0.1' or 'localhost' (the default) or '::1' (the ipv6 loopback address) any connections opened from a remote address which is not one of those addresses will be immediately closed.

WARNING: The plugin used for this module doesn't support any specified MIME type. This module provides 'application/json' parsing on incoming messages.

Version:
  • $$Id: web-socket-server.js 75980 2017-07-18 00:19:25Z chadlia.jerad $$
Author:
  • Matt Weber
Source:

Methods

(static) Server(options)

Construct an instance of WebSocket Server. After invoking this constructor (using new), the user script should set up listeners and then invoke the start() function on this Server. This will create an HTTP server on the local host. The options argument is a JSON object containing the following optional fields:

  • hostInterface: WARNING: The host interface cannot be set with this cordova plugin. This parameter is ignored and is automatically set to 0.0.0.0 (all interfaces)
  • port: The port on which to listen for connections (the default is 80, which is the default HTTP port). Setting port 0 means any free port.
  • receiveType: The MIME type for incoming messages, which defaults to application/json'. See the Client documentation for supported types.
  • sendType: The MIME type for outgoing messages, which defaults to 'application/json'. See the Client documentation for supported types.

    This subclasses EventEmitter, emitting events 'listening' and 'connection'. A typical usage pattern looks like this:

     var webSocket = require('webSocketServer');
     var server = new webSocket.Server({'port':8082});
     server.on('listening', onListening);
     server.on('connection', onConnection);
     server.start();
    

    where onListening is a handler for an event that this Server emits when it is listening for connections, and onConnection is a handler for an event that this Server emits when a client requests a websocket connection and the socket has been successfully established. When the 'connection' event is emitted, it will be passed a Socket object, and the onConnection handler can register a listener for 'message' events on that Socket object, as follows:

    server.on('connection', function(socket) {
        socket.on('message', function(message) {
            console.log(message);
            socket.send('Reply message');
        });
     });
    

    The Socket object also has a close() function that allows the server to close the connection.

    FIXME: Should provide a mechanism to validate the "Origin" header during the connection establishment process on the serverside (against the expected origins) to avoid Cross-Site WebSocket Hijacking attacks.

Parameters:
Name Type Description
options

The options.

Source:

(static) Socket(serverWebSocket, helper, receiveType, sendType)

Construct (using new) a Socket object for the server side of a new connection. This is called by the socketCreated function above whenever a new connection is established at the request of a client. It should not normally be called by the JavaScript programmer. The returned Socket is an event emitter that emits 'message' events.

Parameters:
Name Type Description
serverWebSocket

The Java ServerWebSocket object. NOTE: In this host, the object is the global server object.

helper

NOTE: A socket helper in Cordova is the connection object.

receiveType

The MIME type for incoming messages, which defaults to 'application/json'.

sendType

The MIME type for outgoing messages, which defaults to 'application/json'.

Source:

(static) supportedReceiveTypes()

Return an array of the types supported by the current host for receiveType arguments.

Source:

(static) supportedSendTypes()

Return an array of the types supported by the current host for sendType arguments.

Source: