Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support firefoxos platform. #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@
<source-file src="src/wp8/src/SocketStorage.cs" target-dir="src" />
</platform>



<!-- firefoxos -->
<platform name="firefoxos">
<config-file target="config.xml" parent="/*">
<permission name="tcp-socket" description="Create TCP sockets and communicate over them." />
</config-file>
<js-module src="src/firefoxos/SocketProxy.js" name="SocketPlugin">
<clobbers target="Socket"/>
</js-module>
</platform>
</plugin>
97 changes: 97 additions & 0 deletions src/firefoxos/SocketProxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//TODO: we need add below setting to platforms/firefoxos/www/manifest.webapp
// "type": "privileged",
// "permissions": {
// "tcp-socket": {
// "description" : "Create TCP sockets and communicate over them."
// }
// },
// or add below setting to your cordova project config.xml
// <platform name="firefoxos">
// <permission name="tcp-socket" description="Create TCP sockets and communicate over them." privileged="true"/>
// </platform>

function TcpSocket() {
this.onData = null;
this.onError = null;
this.onClose = null;
this.socket = null;
this.writable = false;
this.state = TcpSocket.State.CLOSED;
}

TcpSocket.State = {};
TcpSocket.State[Socket.State.CLOSED = 0] = "CLOSED";
TcpSocket.State[Socket.State.OPENING = 1] = "OPENING";
TcpSocket.State[Socket.State.OPENED = 2] = "OPENED";
TcpSocket.State[Socket.State.CLOSING = 3] = "CLOSING";

TcpSocket.prototype={
//Ref: https://developer.mozilla.org/en-US/docs/Web/API/TCPSocket
open: function(host, port, onStartRequest, onSocketError){
this.onStartRequest = onStartRequest;
this.onSocketError = onSocketError;
this.state = TcpSocket.State.OPENING;
this.socket = navigator.mozTCPSocket.open(host, port, {binaryType: 'arraybuffer'});

this.socket.onopen = function(){
this.writable = true;
this.state = TcpSocket.State.OPENED;
if(this.onStartRequest)
this.onStartRequest();
}.bind(this);

this.socket.onerror = function(e){
if(this.onSocketError)
this.onSocketError(e.data);
}.bind(this);

this.socket.ondata = function(e){
if(this.onData) {
this.onData(new Uint8Array(e.data));
}
}.bind(this);

this.socket.onclose = function(){
this.state = TcpSocket.State.CLOSED;
if(this.onClose) {
this.onClose();
}
}.bind(this);
},

write: function(data, onSuccess, onError){
if(this.socket && this.writable) {
try{
this.socket.send(data.buffer);
onSuccess();
} catch (ex) {
onError(ex.toString());
}
}
},

shutdownWrite: function(onSuccess, onError){
this.writable = false;
},

close: function(onSuccess, onError){
this.state = TcpSocket.State.CLOSING;
if(this.socket) {
try{
this.socket.close();
this.socket = null;
if(onSuccess) {
onSuccess();
}
} catch (ex) {
if(onError) {
onError(ex.toString());
}
}
}
}
};

module.exports = TcpSocket;

require("cordova/exec/proxy").add("Socket", module.exports);