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.
In your application project directory:
cordova plugin add cordova-plugin-websocket-server
var wsserver = cordova.plugins.wsserver;
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¶m2=value2'
} */
console.log('A user connected from %s', conn.remoteAddr);
},
'onMessage' : function(conn, msg) {
console.log(conn, msg); // msg can be a String (text message) or ArrayBuffer (binary message)
},
'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);
});
Stops the server.
wsserver.stop(function onStop(addr, port) {
console.log('Stopped listening on %s:%d', addr, port);
});
Sends a message to the given connection.
// provide a String to send a text frame (websocket opcode 1)
wsserver.send({'uuid':'8e176b14-a1af-70a7-3e3d-8b341977a16e'}, 'hello friend!');
// provide a TypedArray / ArrayBuffer to send a binary frame (websocket opcode 2)
wsserver.send({'uuid':'8e176b14-a1af-70a7-3e3d-8b341977a16e'}, Uint8Array.from([1, 2, 3, 4]));
Closes a websocket connection. Close event code and reason are optional.
wsserver.close({'uuid':'8e176b14-a1af-70a7-3e3d-8b341977a16e'}, 4000, 'my reason');
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);
}
}
});
It depends on the TooTallNate WebSocket Server.
It depends on the couchbasedeps PocketSocket Server forked from the zwopple PocketSocket Server.
The MIT License