2014-09-26 21:21:24 +08:00
|
|
|
var exec = require('cordova/exec');
|
|
|
|
|
|
|
|
var SOCKET_EVENT = "SOCKET_EVENT";
|
|
|
|
var CORDOVA_SERVICE_NAME = "SocketsForCordova";
|
|
|
|
|
2015-01-03 16:18:14 +08:00
|
|
|
Socket.State = {};
|
|
|
|
Socket.State[Socket.State.CLOSED = 0] = "CLOSED";
|
|
|
|
Socket.State[Socket.State.OPENING = 1] = "OPENING";
|
|
|
|
Socket.State[Socket.State.OPENED = 2] = "OPENED";
|
|
|
|
Socket.State[Socket.State.CLOSING = 3] = "CLOSING";
|
|
|
|
|
2014-09-26 21:21:24 +08:00
|
|
|
function Socket() {
|
2014-12-20 05:53:13 +08:00
|
|
|
this._state = Socket.State.CLOSED;
|
2014-09-26 21:21:24 +08:00
|
|
|
this.onData = null;
|
|
|
|
this.onClose = null;
|
|
|
|
this.onError = null;
|
|
|
|
this.socketKey = guid();
|
|
|
|
}
|
|
|
|
|
2014-10-19 00:46:56 +08:00
|
|
|
Socket.prototype.open = function (host, port, success, error) {
|
2014-09-26 21:21:24 +08:00
|
|
|
|
2015-01-03 16:18:14 +08:00
|
|
|
success = success || function() { };
|
|
|
|
error = error || function() { };
|
|
|
|
|
|
|
|
if (!this._ensureState(Socket.State.CLOSED, error)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-19 01:36:47 +08:00
|
|
|
var _that = this;
|
2014-12-20 05:53:13 +08:00
|
|
|
|
2014-09-26 21:21:24 +08:00
|
|
|
function socketEventHandler(event) {
|
|
|
|
|
|
|
|
var payload = event.payload;
|
|
|
|
|
2014-10-19 01:36:47 +08:00
|
|
|
if (payload.socketKey !== _that.socketKey) {
|
2014-09-26 21:21:24 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-20 05:53:13 +08:00
|
|
|
switch (payload.type) {
|
2014-09-26 21:21:24 +08:00
|
|
|
case "Close":
|
2014-12-20 05:53:13 +08:00
|
|
|
_that._state = Socket.State.CLOSED;
|
2014-09-26 21:21:24 +08:00
|
|
|
window.document.removeEventListener(SOCKET_EVENT, socketEventHandler);
|
2015-01-03 16:18:14 +08:00
|
|
|
_that.onClose(payload.hasError);
|
2014-09-26 21:21:24 +08:00
|
|
|
break;
|
|
|
|
case "DataReceived":
|
2014-11-10 04:02:46 +08:00
|
|
|
_that.onData(new Uint8Array(payload.data));
|
2014-09-26 21:21:24 +08:00
|
|
|
break;
|
|
|
|
case "Error":
|
2014-10-19 01:36:47 +08:00
|
|
|
_that.onError(payload.errorMessage);
|
2014-09-26 21:21:24 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.error("SocketsForCordova: Unknown event type " + payload.type + ", socket key: " + payload.socketKey);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-20 05:53:13 +08:00
|
|
|
_that._state = Socket.State.OPENING;
|
|
|
|
|
2014-09-26 21:21:24 +08:00
|
|
|
exec(
|
2014-12-20 05:53:13 +08:00
|
|
|
function () {
|
|
|
|
_that._state = Socket.State.OPENED;
|
2014-10-19 00:46:56 +08:00
|
|
|
window.document.addEventListener(SOCKET_EVENT, socketEventHandler);
|
2015-01-03 16:18:14 +08:00
|
|
|
success();
|
2014-09-26 21:21:24 +08:00
|
|
|
},
|
2015-01-03 16:18:14 +08:00
|
|
|
function(errorMessage) {
|
|
|
|
_that._state = Socket.State.CLOSED;
|
|
|
|
error(errorMessage);
|
2014-09-26 21:21:24 +08:00
|
|
|
},
|
|
|
|
CORDOVA_SERVICE_NAME,
|
2014-10-19 00:46:56 +08:00
|
|
|
"open",
|
2014-09-26 21:21:24 +08:00
|
|
|
[ this.socketKey, host, port ]);
|
|
|
|
};
|
|
|
|
|
|
|
|
Socket.prototype.write = function (data, success, error) {
|
|
|
|
|
2015-01-03 16:18:14 +08:00
|
|
|
success = success || function() { };
|
|
|
|
error = error || function() { };
|
|
|
|
|
|
|
|
if (!this._ensureState(Socket.State.OPENED, error)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-10 04:02:46 +08:00
|
|
|
var dataToWrite = data instanceof Uint8Array
|
2014-09-26 21:21:24 +08:00
|
|
|
? Socket._copyToArray(data)
|
|
|
|
: data;
|
|
|
|
|
|
|
|
exec(
|
2015-01-03 16:18:14 +08:00
|
|
|
success,
|
|
|
|
error,
|
2014-09-26 21:21:24 +08:00
|
|
|
CORDOVA_SERVICE_NAME,
|
|
|
|
"write",
|
|
|
|
[ this.socketKey, dataToWrite ]);
|
|
|
|
};
|
|
|
|
|
2015-01-03 16:18:14 +08:00
|
|
|
Socket.prototype.shutdownWrite = function (success, error) {
|
|
|
|
|
|
|
|
success = success || function() { };
|
|
|
|
error = error || function() { };
|
|
|
|
|
|
|
|
if (!this._ensureState(Socket.State.OPENED, error)) {
|
|
|
|
return;
|
2014-09-26 21:21:24 +08:00
|
|
|
}
|
|
|
|
|
2014-10-13 04:13:39 +08:00
|
|
|
exec(
|
2015-01-03 16:18:14 +08:00
|
|
|
success,
|
|
|
|
error,
|
2014-12-20 05:53:13 +08:00
|
|
|
CORDOVA_SERVICE_NAME,
|
|
|
|
"shutdownWrite",
|
|
|
|
[ this.socketKey ]);
|
2014-10-13 04:13:39 +08:00
|
|
|
};
|
|
|
|
|
2015-01-03 16:18:14 +08:00
|
|
|
Socket.prototype.close = function (success, error) {
|
|
|
|
|
|
|
|
success = success || function() { };
|
|
|
|
error = error || function() { };
|
|
|
|
|
|
|
|
if (!this._ensureState(Socket.State.OPENED, error)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._state = Socket.State.CLOSING;
|
|
|
|
|
2014-09-26 21:21:24 +08:00
|
|
|
exec(
|
2015-01-03 16:18:14 +08:00
|
|
|
success,
|
|
|
|
error,
|
2014-12-20 05:53:13 +08:00
|
|
|
CORDOVA_SERVICE_NAME,
|
|
|
|
"close",
|
|
|
|
[ this.socketKey ]);
|
2014-09-26 21:21:24 +08:00
|
|
|
};
|
|
|
|
|
2014-12-20 05:53:13 +08:00
|
|
|
Object.defineProperty(Socket.prototype, "state", {
|
|
|
|
get: function () {
|
|
|
|
return this._state;
|
|
|
|
},
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true
|
|
|
|
});
|
|
|
|
|
2015-01-03 16:18:14 +08:00
|
|
|
Socket.prototype._ensureState = function(requiredState, errorCallback) {
|
|
|
|
var state = this._state;
|
|
|
|
if (state != requiredState) {
|
|
|
|
window.setTimeout(function() {
|
|
|
|
errorCallback("Invalid operation for this socket state: " + Socket.State[state]);
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2014-12-20 05:53:13 +08:00
|
|
|
|
|
|
|
Socket.dispatchEvent = function (event) {
|
2014-09-26 21:21:24 +08:00
|
|
|
var eventReceive = document.createEvent('Events');
|
|
|
|
eventReceive.initEvent(SOCKET_EVENT, true, true);
|
|
|
|
eventReceive.payload = event;
|
|
|
|
|
|
|
|
document.dispatchEvent(eventReceive);
|
|
|
|
};
|
|
|
|
|
2015-01-03 16:18:14 +08:00
|
|
|
Socket._copyToArray = function (array) {
|
|
|
|
var outputArray = new Array(array.length);
|
|
|
|
for (var i = 0; i < array.length; i++) {
|
|
|
|
outputArray[i] = array[i];
|
|
|
|
}
|
|
|
|
return outputArray;
|
|
|
|
};
|
|
|
|
|
2014-12-20 05:53:13 +08:00
|
|
|
var guid = (function () {
|
2014-09-26 21:21:24 +08:00
|
|
|
function s4() {
|
|
|
|
return Math.floor((1 + Math.random()) * 0x10000)
|
|
|
|
.toString(16)
|
|
|
|
.substring(1);
|
|
|
|
}
|
2014-12-20 05:53:13 +08:00
|
|
|
|
|
|
|
return function () {
|
2014-09-26 21:21:24 +08:00
|
|
|
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
|
|
|
|
s4() + '-' + s4() + s4() + s4();
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Register event dispatcher for Windows Phone
|
|
|
|
if (navigator.userAgent.match(/iemobile/i)) {
|
2014-12-20 05:53:13 +08:00
|
|
|
window.document.addEventListener("deviceready", function () {
|
2014-09-26 21:21:24 +08:00
|
|
|
exec(
|
|
|
|
Socket.dispatchEvent,
|
2014-12-20 05:53:13 +08:00
|
|
|
function (errorMessage) {
|
2014-09-26 21:21:24 +08:00
|
|
|
console.error("SocketsForCordova: Cannot register WP event dispatcher, Error: " + errorMessage);
|
|
|
|
},
|
|
|
|
CORDOVA_SERVICE_NAME,
|
|
|
|
"registerWPEventDispatcher",
|
|
|
|
[ ]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Socket;
|