Skip to content

astronomer00/cordova-plugin-websocket-server

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

1.4.1

  • new tcpNoDelay option

1.4.0

  • onStart, onDidNotStart and onStop handlers replaced with success and failure callbacks
  • added generic onFailure handler (assume the server is unexpectedly stopped in this handler)
  • [iOS] fixed crash on stop and close (dealloc)

1.3.1

  • adding state 'open' or 'closed' to the conn object

1.3.0

  • getInterfaces returns the ipv4 and ipv6 addresses organized by network interface
  • onOpen, onMessage and onClose handlers share the same conn instances
  • [Android] switching to com.pusher:java-websocket:1.4.1
  • [iOS] IPv6 support 30a98b0

1.2.1

  • [iOS] fixed crash (error retrieving URL query string)

1.2.0

  • new onDidNotStart handler in the start method
  • getInterfaces returns ipv4 and ipv6 addresses

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',
         'acceptedProtocol' : 'my-protocol-v1',
         '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) {
        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 // enable/disable Nagle's algorithm. false by default.
}, 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

  • Java 47.9%
  • Swift 46.6%
  • JavaScript 5.4%
  • Objective-C 0.1%