From df1d4f8272dbf3faf2d3d71a6045e7c78ca3225b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Obr=C3=A1til?= Date: Fri, 19 Dec 2014 22:53:13 +0100 Subject: [PATCH] Proposal impleementation of state property on Socket prototype --- socket.js | 90 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 55 insertions(+), 35 deletions(-) diff --git a/socket.js b/socket.js index 7b23569..5de7e77 100644 --- a/socket.js +++ b/socket.js @@ -4,6 +4,7 @@ var SOCKET_EVENT = "SOCKET_EVENT"; var CORDOVA_SERVICE_NAME = "SocketsForCordova"; function Socket() { + this._state = Socket.State.CLOSED; this.onData = null; this.onClose = null; this.onError = null; @@ -13,6 +14,7 @@ function Socket() { Socket.prototype.open = function (host, port, success, error) { var _that = this; + function socketEventHandler(event) { var payload = event.payload; @@ -21,9 +23,10 @@ Socket.prototype.open = function (host, port, success, error) { return; } - switch(payload.type) { + switch (payload.type) { case "Close": //console.debug("SocketsForCordova: Close event, socket key: " + payload.socketKey); + _that._state = Socket.State.CLOSED; window.document.removeEventListener(SOCKET_EVENT, socketEventHandler); _that.onClose(); break; @@ -41,14 +44,17 @@ Socket.prototype.open = function (host, port, success, error) { } } + _that._state = Socket.State.OPENING; + exec( - function() { + function () { //console.debug("SocketsForCordova: Socket successfully opened."); + _that._state = Socket.State.OPENED; window.document.addEventListener(SOCKET_EVENT, socketEventHandler); if (success) success(); }, - function(errorMessage) { + function (errorMessage) { //console.error("SocketsForCordova: Error during opening socket. Error: " + errorMessage); if (error) error(errorMessage); @@ -65,12 +71,12 @@ Socket.prototype.write = function (data, success, error) { : data; exec( - function() { + function () { //console.debug("SocketsForCordova: Data successfully written to socket. Number of bytes: " + data.length); if (success) success(); }, - function(errorMessage) { + function (errorMessage) { //console.error("SocketsForCordova: Error during writing data to socket. Error: " + errorMessage); if (error) error(errorMessage); @@ -80,7 +86,7 @@ Socket.prototype.write = function (data, success, error) { [ this.socketKey, dataToWrite ]); }; -Socket._copyToArray = function(array) { +Socket._copyToArray = function (array) { var outputArray = new Array(array.length); for (var i = 0; i < array.length; i++) { outputArray[i] = array[i]; @@ -90,39 +96,52 @@ Socket._copyToArray = function(array) { Socket.prototype.shutdownWrite = function (success, error) { exec( - function() { + function () { //console.debug("SocketsForCordova: Shutdown write successfully called."); - if (success) - success(); - }, - function(errorMessage) { + if (success) + success(); + }, + function (errorMessage) { //console.error("SocketsForCordova: Error when call shutdownWrite on socket. Error: " + errorMessage); - if (error) - error(errorMessage); - }, - CORDOVA_SERVICE_NAME, - "shutdownWrite", - [ this.socketKey ]); + if (error) + error(errorMessage); + }, + CORDOVA_SERVICE_NAME, + "shutdownWrite", + [ this.socketKey ]); }; Socket.prototype.close = function () { exec( - function() { - //console.debug("SocketsForCordova: Close successfully called."); - if (success) - success(); - }, - function(errorMessage) { - //console.error("SocketsForCordova: Error when call close on socket. Error: " + errorMessage); - if (error) - error(errorMessage); - }, - CORDOVA_SERVICE_NAME, - "close", - [ this.socketKey ]); + function () { + //console.debug("SocketsForCordova: Close successfully called."); + if (success) + success(); + }, + function (errorMessage) { + //console.error("SocketsForCordova: Error when call close on socket. Error: " + errorMessage); + if (error) + error(errorMessage); + }, + CORDOVA_SERVICE_NAME, + "close", + [ 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'); eventReceive.initEvent(SOCKET_EVENT, true, true); eventReceive.payload = event; @@ -130,13 +149,14 @@ Socket.dispatchEvent = function(event) { document.dispatchEvent(eventReceive); }; -var guid = (function() { +var guid = (function () { function s4() { return Math.floor((1 + Math.random()) * 0x10000) .toString(16) .substring(1); } - return function() { + + return function () { return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4(); }; @@ -144,10 +164,10 @@ var guid = (function() { // Register event dispatcher for Windows Phone if (navigator.userAgent.match(/iemobile/i)) { - window.document.addEventListener("deviceready", function() { + window.document.addEventListener("deviceready", function () { exec( Socket.dispatchEvent, - function(errorMessage) { + function (errorMessage) { console.error("SocketsForCordova: Cannot register WP event dispatcher, Error: " + errorMessage); }, CORDOVA_SERVICE_NAME,