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.
Installation
In your application project directory:
cordova plugin add cordova-plugin-websocket-server
Usage
var wsserver = cordovapluginswsserver;
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').
wsserverstartport // WebSocket Server handlers { console; } // WebSocket Connection handlers { /* conn: { 'uuid' : '8e176b14-a1af-70a7-3e3d-8b341977a16e', 'remoteAddr' : '192.168.1.10', 'httpFields' : {...}, 'resource' : '/?param1=value1¶m2=value2' } */ console; } { console; // msg can be a String (text message) or ArrayBuffer (binary message) } { console; } // 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. { console;} { console;};
stop(success,failure)
Stops the server.
wsserver;
send(conn, msg)
Sends a message to the given connection.
// provide a String to send a text frame (websocket opcode 1)wsserver; // provide a TypedArray / ArrayBuffer to send a binary frame (websocket opcode 2)wsserver;
close(conn, code, reason)
Closes a websocket connection. Close event code and reason are optional.
wsserver;
getInterfaces(callback)
Returns the non-loopback IPv4 and IPv6 network interfaces.
wsserver;
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