Skip to content

Hippoi/cordova-plugin-websocket-server

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cordova WebSocket Server Plugin

This plugin allows you to run a single, lightweight, barebone WebSocket Server from applications developed using PhoneGap/Cordova 3.0 or newer.

This is not a background service. When the cordova view is destroyed/terminated, the server is stopped.

CHANGELOG

Installation

In your application project directory:

cordova plugin add cordova-plugin-websocket-server

Usage

var wsserver = cordova.plugins.wsserver;

start(port, options, success, failure)

Starts the server on the given port (0 means any free port). Binds to all available network interfaces ('0.0.0.0').

 wsserver.start(port, {
    // WebSocket Server handlers
    'onFailure' :  function(addr, port, reason) {
        console.log('Stopped listening on %s:%d. Reason: %s', addr, port, reason);
    },
    // WebSocket Connection handlers
    'onOpen' : function(conn) {
        /* conn: {
         'uuid' : '8e176b14-a1af-70a7-3e3d-8b341977a16e',
         'remoteAddr' : '192.168.1.10',
         'httpFields' : {...},
         'resource' : '/?param1=value1&param2=value2'
         } */
        console.log('A user connected from %s', conn.remoteAddr);
    },
    'onMessage' : function(conn, msg) {
        console.log(conn, msg);
    },
    'onClose' : function(conn, code, reason, wasClean) {
        console.log('A user disconnected from %s', conn.remoteAddr);
    },
    // Other options
    'origins' : [ 'file://' ], // validates the 'Origin' HTTP Header.
    'protocols' : [ 'my-protocol-v1', 'my-protocol-v2' ], // validates the 'Sec-WebSocket-Protocol' HTTP Header.
    'tcpNoDelay' : true // disables Nagle's algorithm.
}, function onStart(addr, port) {
    console.log('Listening on %s:%d', addr, port);
}, function onDidNotStart(reason) {
    console.log('Did not start. Reason: %s', reason);
});

stop(success,failure)

Stops the server.

wsserver.stop(function onStop(addr, port) {
    console.log('Stopped listening on %s:%d', addr, port);
});

send(conn, msg)

Sends a message to the given connection.

wsserver.send({'uuid':'8e176b14-a1af-70a7-3e3d-8b341977a16e'}, msg);

close(conn, code, reason)

Closes a websocket connection. Close event code and reason are optional.

wsserver.close({'uuid':'8e176b14-a1af-70a7-3e3d-8b341977a16e'}, 4000, 'my reason');

getInterfaces(callback)

Returns the non-loopback IPv4 and IPv6 network interfaces.

wsserver.getInterfaces(function(result) {
    for (var interface in result) {
        if (result.hasOwnProperty(interface)) {
            console.log('interface', interface);
            console.log('ipv4', result[interface].ipv4Addresses);
            console.log('ipv6', result[interface].ipv6Addresses);
        }
    }
});

Credits

Android

It depends on the TooTallNate WebSocket Server.

iOS

It depends on the couchbasedeps PocketSocket Server forked from the zwopple PocketSocket Server.

Licence

The MIT License

About

Cordova WebSocket Server Plugin

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 49.0%
  • Java 45.3%
  • JavaScript 5.5%
  • Objective-C 0.2%