Proposal impleementation of state property on Socket prototype

This commit is contained in:
Martin Obrátil 2014-12-19 22:53:13 +01:00
parent 88bc09fb83
commit df1d4f8272

View File

@ -4,6 +4,7 @@ var SOCKET_EVENT = "SOCKET_EVENT";
var CORDOVA_SERVICE_NAME = "SocketsForCordova"; var CORDOVA_SERVICE_NAME = "SocketsForCordova";
function Socket() { function Socket() {
this._state = Socket.State.CLOSED;
this.onData = null; this.onData = null;
this.onClose = null; this.onClose = null;
this.onError = null; this.onError = null;
@ -13,6 +14,7 @@ function Socket() {
Socket.prototype.open = function (host, port, success, error) { Socket.prototype.open = function (host, port, success, error) {
var _that = this; var _that = this;
function socketEventHandler(event) { function socketEventHandler(event) {
var payload = event.payload; var payload = event.payload;
@ -21,9 +23,10 @@ Socket.prototype.open = function (host, port, success, error) {
return; return;
} }
switch(payload.type) { switch (payload.type) {
case "Close": case "Close":
//console.debug("SocketsForCordova: Close event, socket key: " + payload.socketKey); //console.debug("SocketsForCordova: Close event, socket key: " + payload.socketKey);
_that._state = Socket.State.CLOSED;
window.document.removeEventListener(SOCKET_EVENT, socketEventHandler); window.document.removeEventListener(SOCKET_EVENT, socketEventHandler);
_that.onClose(); _that.onClose();
break; break;
@ -41,14 +44,17 @@ Socket.prototype.open = function (host, port, success, error) {
} }
} }
_that._state = Socket.State.OPENING;
exec( exec(
function() { function () {
//console.debug("SocketsForCordova: Socket successfully opened."); //console.debug("SocketsForCordova: Socket successfully opened.");
_that._state = Socket.State.OPENED;
window.document.addEventListener(SOCKET_EVENT, socketEventHandler); window.document.addEventListener(SOCKET_EVENT, socketEventHandler);
if (success) if (success)
success(); success();
}, },
function(errorMessage) { function (errorMessage) {
//console.error("SocketsForCordova: Error during opening socket. Error: " + errorMessage); //console.error("SocketsForCordova: Error during opening socket. Error: " + errorMessage);
if (error) if (error)
error(errorMessage); error(errorMessage);
@ -65,12 +71,12 @@ Socket.prototype.write = function (data, success, error) {
: data; : data;
exec( exec(
function() { function () {
//console.debug("SocketsForCordova: Data successfully written to socket. Number of bytes: " + data.length); //console.debug("SocketsForCordova: Data successfully written to socket. Number of bytes: " + data.length);
if (success) if (success)
success(); success();
}, },
function(errorMessage) { function (errorMessage) {
//console.error("SocketsForCordova: Error during writing data to socket. Error: " + errorMessage); //console.error("SocketsForCordova: Error during writing data to socket. Error: " + errorMessage);
if (error) if (error)
error(errorMessage); error(errorMessage);
@ -80,7 +86,7 @@ Socket.prototype.write = function (data, success, error) {
[ this.socketKey, dataToWrite ]); [ this.socketKey, dataToWrite ]);
}; };
Socket._copyToArray = function(array) { Socket._copyToArray = function (array) {
var outputArray = new Array(array.length); var outputArray = new Array(array.length);
for (var i = 0; i < array.length; i++) { for (var i = 0; i < array.length; i++) {
outputArray[i] = array[i]; outputArray[i] = array[i];
@ -90,39 +96,52 @@ Socket._copyToArray = function(array) {
Socket.prototype.shutdownWrite = function (success, error) { Socket.prototype.shutdownWrite = function (success, error) {
exec( exec(
function() { function () {
//console.debug("SocketsForCordova: Shutdown write successfully called."); //console.debug("SocketsForCordova: Shutdown write successfully called.");
if (success) if (success)
success(); success();
}, },
function(errorMessage) { function (errorMessage) {
//console.error("SocketsForCordova: Error when call shutdownWrite on socket. Error: " + errorMessage); //console.error("SocketsForCordova: Error when call shutdownWrite on socket. Error: " + errorMessage);
if (error) if (error)
error(errorMessage); error(errorMessage);
}, },
CORDOVA_SERVICE_NAME, CORDOVA_SERVICE_NAME,
"shutdownWrite", "shutdownWrite",
[ this.socketKey ]); [ this.socketKey ]);
}; };
Socket.prototype.close = function () { Socket.prototype.close = function () {
exec( exec(
function() { function () {
//console.debug("SocketsForCordova: Close successfully called."); //console.debug("SocketsForCordova: Close successfully called.");
if (success) if (success)
success(); success();
}, },
function(errorMessage) { function (errorMessage) {
//console.error("SocketsForCordova: Error when call close on socket. Error: " + errorMessage); //console.error("SocketsForCordova: Error when call close on socket. Error: " + errorMessage);
if (error) if (error)
error(errorMessage); error(errorMessage);
}, },
CORDOVA_SERVICE_NAME, CORDOVA_SERVICE_NAME,
"close", "close",
[ this.socketKey ]); [ this.socketKey ]);
}; };
Socket.dispatchEvent = function(event) { Object.defineProperty(Socket.prototype, "state", {
get: function () {
return this._state;
},
enumerable: true,
configurable: true
});
Socket.State = {};
Socket.State[Socket.State.CLOSED = 0] = "CLOSED";
Socket.State[Socket.State.OPENING = 1] = "OPENING";
Socket.State[Socket.State.OPENED = 2] = "OPENED";
Socket.dispatchEvent = function (event) {
var eventReceive = document.createEvent('Events'); var eventReceive = document.createEvent('Events');
eventReceive.initEvent(SOCKET_EVENT, true, true); eventReceive.initEvent(SOCKET_EVENT, true, true);
eventReceive.payload = event; eventReceive.payload = event;
@ -130,13 +149,14 @@ Socket.dispatchEvent = function(event) {
document.dispatchEvent(eventReceive); document.dispatchEvent(eventReceive);
}; };
var guid = (function() { var guid = (function () {
function s4() { function s4() {
return Math.floor((1 + Math.random()) * 0x10000) return Math.floor((1 + Math.random()) * 0x10000)
.toString(16) .toString(16)
.substring(1); .substring(1);
} }
return function() {
return function () {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4(); s4() + '-' + s4() + s4() + s4();
}; };
@ -144,10 +164,10 @@ var guid = (function() {
// Register event dispatcher for Windows Phone // Register event dispatcher for Windows Phone
if (navigator.userAgent.match(/iemobile/i)) { if (navigator.userAgent.match(/iemobile/i)) {
window.document.addEventListener("deviceready", function() { window.document.addEventListener("deviceready", function () {
exec( exec(
Socket.dispatchEvent, Socket.dispatchEvent,
function(errorMessage) { function (errorMessage) {
console.error("SocketsForCordova: Cannot register WP event dispatcher, Error: " + errorMessage); console.error("SocketsForCordova: Cannot register WP event dispatcher, Error: " + errorMessage);
}, },
CORDOVA_SERVICE_NAME, CORDOVA_SERVICE_NAME,