Added CLOSING state, added state chcking before each operation, small bug fixes.
This commit is contained in:
parent
df1d4f8272
commit
53372f1de9
120
socket.js
120
socket.js
@ -3,6 +3,12 @@ var exec = require('cordova/exec');
|
|||||||
var SOCKET_EVENT = "SOCKET_EVENT";
|
var SOCKET_EVENT = "SOCKET_EVENT";
|
||||||
var CORDOVA_SERVICE_NAME = "SocketsForCordova";
|
var CORDOVA_SERVICE_NAME = "SocketsForCordova";
|
||||||
|
|
||||||
|
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";
|
||||||
|
|
||||||
function Socket() {
|
function Socket() {
|
||||||
this._state = Socket.State.CLOSED;
|
this._state = Socket.State.CLOSED;
|
||||||
this.onData = null;
|
this.onData = null;
|
||||||
@ -13,6 +19,13 @@ function Socket() {
|
|||||||
|
|
||||||
Socket.prototype.open = function (host, port, success, error) {
|
Socket.prototype.open = function (host, port, success, error) {
|
||||||
|
|
||||||
|
success = success || function() { };
|
||||||
|
error = error || function() { };
|
||||||
|
|
||||||
|
if (!this._ensureState(Socket.State.CLOSED, error)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var _that = this;
|
var _that = this;
|
||||||
|
|
||||||
function socketEventHandler(event) {
|
function socketEventHandler(event) {
|
||||||
@ -25,17 +38,14 @@ Socket.prototype.open = function (host, port, success, error) {
|
|||||||
|
|
||||||
switch (payload.type) {
|
switch (payload.type) {
|
||||||
case "Close":
|
case "Close":
|
||||||
//console.debug("SocketsForCordova: Close event, socket key: " + payload.socketKey);
|
|
||||||
_that._state = Socket.State.CLOSED;
|
_that._state = Socket.State.CLOSED;
|
||||||
window.document.removeEventListener(SOCKET_EVENT, socketEventHandler);
|
window.document.removeEventListener(SOCKET_EVENT, socketEventHandler);
|
||||||
_that.onClose();
|
_that.onClose(payload.hasError);
|
||||||
break;
|
break;
|
||||||
case "DataReceived":
|
case "DataReceived":
|
||||||
//console.debug("SocketsForCordova: DataReceived event, socket key: " + payload.socketKey);
|
|
||||||
_that.onData(new Uint8Array(payload.data));
|
_that.onData(new Uint8Array(payload.data));
|
||||||
break;
|
break;
|
||||||
case "Error":
|
case "Error":
|
||||||
//console.debug("SocketsForCordova: Error event, socket key: " + payload.socketKey);
|
|
||||||
_that.onError(payload.errorMessage);
|
_that.onError(payload.errorMessage);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -48,15 +58,12 @@ Socket.prototype.open = function (host, port, success, error) {
|
|||||||
|
|
||||||
exec(
|
exec(
|
||||||
function () {
|
function () {
|
||||||
//console.debug("SocketsForCordova: Socket successfully opened.");
|
|
||||||
_that._state = Socket.State.OPENED;
|
_that._state = Socket.State.OPENED;
|
||||||
window.document.addEventListener(SOCKET_EVENT, socketEventHandler);
|
window.document.addEventListener(SOCKET_EVENT, socketEventHandler);
|
||||||
if (success)
|
|
||||||
success();
|
success();
|
||||||
},
|
},
|
||||||
function (errorMessage) {
|
function(errorMessage) {
|
||||||
//console.error("SocketsForCordova: Error during opening socket. Error: " + errorMessage);
|
_that._state = Socket.State.CLOSED;
|
||||||
if (error)
|
|
||||||
error(errorMessage);
|
error(errorMessage);
|
||||||
},
|
},
|
||||||
CORDOVA_SERVICE_NAME,
|
CORDOVA_SERVICE_NAME,
|
||||||
@ -66,63 +73,56 @@ Socket.prototype.open = function (host, port, success, error) {
|
|||||||
|
|
||||||
Socket.prototype.write = function (data, success, error) {
|
Socket.prototype.write = function (data, success, error) {
|
||||||
|
|
||||||
|
success = success || function() { };
|
||||||
|
error = error || function() { };
|
||||||
|
|
||||||
|
if (!this._ensureState(Socket.State.OPENED, error)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var dataToWrite = data instanceof Uint8Array
|
var dataToWrite = data instanceof Uint8Array
|
||||||
? Socket._copyToArray(data)
|
? Socket._copyToArray(data)
|
||||||
: data;
|
: data;
|
||||||
|
|
||||||
exec(
|
exec(
|
||||||
function () {
|
success,
|
||||||
//console.debug("SocketsForCordova: Data successfully written to socket. Number of bytes: " + data.length);
|
error,
|
||||||
if (success)
|
|
||||||
success();
|
|
||||||
},
|
|
||||||
function (errorMessage) {
|
|
||||||
//console.error("SocketsForCordova: Error during writing data to socket. Error: " + errorMessage);
|
|
||||||
if (error)
|
|
||||||
error(errorMessage);
|
|
||||||
},
|
|
||||||
CORDOVA_SERVICE_NAME,
|
CORDOVA_SERVICE_NAME,
|
||||||
"write",
|
"write",
|
||||||
[ this.socketKey, dataToWrite ]);
|
[ this.socketKey, dataToWrite ]);
|
||||||
};
|
};
|
||||||
|
|
||||||
Socket._copyToArray = function (array) {
|
|
||||||
var outputArray = new Array(array.length);
|
|
||||||
for (var i = 0; i < array.length; i++) {
|
|
||||||
outputArray[i] = array[i];
|
|
||||||
}
|
|
||||||
return outputArray;
|
|
||||||
};
|
|
||||||
|
|
||||||
Socket.prototype.shutdownWrite = function (success, error) {
|
Socket.prototype.shutdownWrite = function (success, error) {
|
||||||
|
|
||||||
|
success = success || function() { };
|
||||||
|
error = error || function() { };
|
||||||
|
|
||||||
|
if (!this._ensureState(Socket.State.OPENED, error)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
exec(
|
exec(
|
||||||
function () {
|
success,
|
||||||
//console.debug("SocketsForCordova: Shutdown write successfully called.");
|
error,
|
||||||
if (success)
|
|
||||||
success();
|
|
||||||
},
|
|
||||||
function (errorMessage) {
|
|
||||||
//console.error("SocketsForCordova: Error when call shutdownWrite on socket. Error: " + errorMessage);
|
|
||||||
if (error)
|
|
||||||
error(errorMessage);
|
|
||||||
},
|
|
||||||
CORDOVA_SERVICE_NAME,
|
CORDOVA_SERVICE_NAME,
|
||||||
"shutdownWrite",
|
"shutdownWrite",
|
||||||
[ this.socketKey ]);
|
[ this.socketKey ]);
|
||||||
};
|
};
|
||||||
|
|
||||||
Socket.prototype.close = function () {
|
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;
|
||||||
|
|
||||||
exec(
|
exec(
|
||||||
function () {
|
success,
|
||||||
//console.debug("SocketsForCordova: Close successfully called.");
|
error,
|
||||||
if (success)
|
|
||||||
success();
|
|
||||||
},
|
|
||||||
function (errorMessage) {
|
|
||||||
//console.error("SocketsForCordova: Error when call close on socket. Error: " + errorMessage);
|
|
||||||
if (error)
|
|
||||||
error(errorMessage);
|
|
||||||
},
|
|
||||||
CORDOVA_SERVICE_NAME,
|
CORDOVA_SERVICE_NAME,
|
||||||
"close",
|
"close",
|
||||||
[ this.socketKey ]);
|
[ this.socketKey ]);
|
||||||
@ -136,10 +136,18 @@ Object.defineProperty(Socket.prototype, "state", {
|
|||||||
configurable: true
|
configurable: true
|
||||||
});
|
});
|
||||||
|
|
||||||
Socket.State = {};
|
Socket.prototype._ensureState = function(requiredState, errorCallback) {
|
||||||
Socket.State[Socket.State.CLOSED = 0] = "CLOSED";
|
var state = this._state;
|
||||||
Socket.State[Socket.State.OPENING = 1] = "OPENING";
|
if (state != requiredState) {
|
||||||
Socket.State[Socket.State.OPENED = 2] = "OPENED";
|
window.setTimeout(function() {
|
||||||
|
errorCallback("Invalid operation for this socket state: " + Socket.State[state]);
|
||||||
|
});
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
Socket.dispatchEvent = function (event) {
|
Socket.dispatchEvent = function (event) {
|
||||||
var eventReceive = document.createEvent('Events');
|
var eventReceive = document.createEvent('Events');
|
||||||
@ -149,6 +157,14 @@ Socket.dispatchEvent = function (event) {
|
|||||||
document.dispatchEvent(eventReceive);
|
document.dispatchEvent(eventReceive);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Socket._copyToArray = function (array) {
|
||||||
|
var outputArray = new Array(array.length);
|
||||||
|
for (var i = 0; i < array.length; i++) {
|
||||||
|
outputArray[i] = array[i];
|
||||||
|
}
|
||||||
|
return outputArray;
|
||||||
|
};
|
||||||
|
|
||||||
var guid = (function () {
|
var guid = (function () {
|
||||||
function s4() {
|
function s4() {
|
||||||
return Math.floor((1 + Math.random()) * 0x10000)
|
return Math.floor((1 + Math.random()) * 0x10000)
|
||||||
|
Loading…
Reference in New Issue
Block a user