From 26a14948e3f8ac3ec09d7d3634629f95aacf4af1 Mon Sep 17 00:00:00 2001 From: kitolog Date: Tue, 27 Mar 2018 11:14:43 +0300 Subject: [PATCH] Fixed cordova js bridge implementation, fixed iOS open timeout --- README.md | 6 +- package.json | 2 +- plugin.xml | 6 +- socket.js | 406 +++++++++--------- src/ios/.DS_Store | Bin 6148 -> 10244 bytes .../SocketsForCordova/Classes/SocketAdapter.m | 3 +- 6 files changed, 217 insertions(+), 206 deletions(-) diff --git a/README.md b/README.md index 0098fa3..1b752e8 100644 --- a/README.md +++ b/README.md @@ -158,5 +158,7 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ## What's new -1.2.3 - fixed ios socket closing crashes -1.5.0 - added ios open and write timeouts, changed js errors format \ No newline at end of file +1.2.3 - fixed iOS socket closing crashes +1.5.0 - added iOS and Android open and write timeouts, changed js errors format +1.5.1 - fixed cordova js bridge implementation +1.5.2 - fixed iOS open timeout \ No newline at end of file diff --git a/package.json b/package.json index fb7bcd6..79d0bf2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cordova-plugin-socket-tcp", - "version": "1.5.0", + "version": "1.5.2", "description": "This Cordova plugin provides JavaScript API, that allows you to communicate with server through TCP protocol. Currently we support these platforms: iOS, Android, WP8.", "cordova": { "platforms": [ diff --git a/plugin.xml b/plugin.xml index ab4e9d0..981093a 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,6 +1,6 @@ - + SocketsForCordova This Cordova plugin provides JavaScript API, that allows you to communicate with server through TCP protocol. @@ -49,7 +49,6 @@ - @@ -65,7 +64,4 @@ - - - diff --git a/socket.js b/socket.js index 5d6a5c2..f5a362e 100644 --- a/socket.js +++ b/socket.js @@ -1,221 +1,233 @@ -cordova.define("cordova-plugin-socket-tcp.Socket", function(require, exports, module) { - /** - * Copyright (c) 2015, Blocshop s.r.o. - * All rights reserved. - * - * Redistribution and use in source and binary forms are permitted - * provided that the above copyright notice and this paragraph are - * duplicated in all such forms and that any documentation, - * advertising materials, and other materials related to such - * distribution and use acknowledge that the software was developed - * by the Blocshop s.r.o.. The name of the - * Blocshop s.r.o. may not be used to endorse or promote products derived - * from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. - */ +/** + * Copyright (c) 2015, Blocshop s.r.o. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by the Blocshop s.r.o.. The name of the + * Blocshop s.r.o. may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. + */ - var exec = require('cordova/exec'); +var exec = require('cordova/exec'); - var SOCKET_EVENT = "SOCKET_EVENT"; - var CORDOVA_SERVICE_NAME = "SocketsForCordova"; +var SOCKET_EVENT = "SOCKET_EVENT"; +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"; +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"; - Socket.ErrorType = {}; - Socket.ErrorType[Socket.ErrorType.GENERAL = 0] = "general"; - Socket.ErrorType[Socket.ErrorType.OPEN_TIMEOUT = 1] = "openTimeout"; - Socket.ErrorType[Socket.ErrorType.WRITE_TIMEOUT = 2] = "writeTimeout"; +Socket.ErrorType = {}; +Socket.ErrorType[Socket.ErrorType.GENERAL = 0] = "general"; +Socket.ErrorType[Socket.ErrorType.OPEN_TIMEOUT = 1] = "openTimeout"; +Socket.ErrorType[Socket.ErrorType.WRITE_TIMEOUT = 2] = "writeTimeout"; - function Socket() { - this._state = Socket.State.CLOSED; - this.onData = null; - this.onClose = null; - this.onError = null; - this.socketKey = guid(); +function Socket() { + this._state = Socket.State.CLOSED; + this.onData = null; + this.onClose = null; + this.onError = null; + this.socketKey = guid(); +} + +Socket.prototype.open = function (host, port, success, error) { + + success = success || function () { + }; + error = error || function () { + }; + + if (!this._ensureState(Socket.State.CLOSED, error)) { + return; } - Socket.prototype.open = function (host, port, success, error) { + var _that = this; - success = success || function() { }; - error = error || function() { }; + function socketEventHandler(event) { - if (!this._ensureState(Socket.State.CLOSED, error)) { + var payload = event.payload; + + if (payload.socketKey !== _that.socketKey) { return; } - var _that = this; - - function socketEventHandler(event) { - - var payload = event.payload; - - if (payload.socketKey !== _that.socketKey) { - return; - } - - switch (payload.type) { - case "Close": - _that._state = Socket.State.CLOSED; - window.document.removeEventListener(SOCKET_EVENT, socketEventHandler); - _that.onClose(payload.hasError); - break; - case "DataReceived": - _that.onData(new Uint8Array(payload.data)); - break; - case "Error": - _that.onError(payload); - break; - default: - console.error("SocketsForCordova: Unknown event type " + payload.type + ", socket key: " + payload.socketKey); - break; - } - } - - _that._state = Socket.State.OPENING; - - exec( - function () { - _that._state = Socket.State.OPENED; - window.document.addEventListener(SOCKET_EVENT, socketEventHandler); - success(); - }, - function(errorMessage) { + switch (payload.type) { + case "Close": _that._state = Socket.State.CLOSED; - error(errorMessage); - }, - CORDOVA_SERVICE_NAME, - "open", - [ this.socketKey, host, port ]); - }; - - Socket.prototype.write = function (data, success, error) { - - success = success || function() { }; - error = error || function() { }; - - if (!this._ensureState(Socket.State.OPENED, error)) { - return; + window.document.removeEventListener(SOCKET_EVENT, socketEventHandler); + _that.onClose(payload.hasError); + break; + case "DataReceived": + _that.onData(new Uint8Array(payload.data)); + break; + case "Error": + _that.onError(payload); + break; + default: + console.error("SocketsForCordova: Unknown event type " + payload.type + ", socket key: " + payload.socketKey); + break; } + } - var dataToWrite = data instanceof Uint8Array - ? Socket._copyToArray(data) - : data; + _that._state = Socket.State.OPENING; - exec( - success, - error, - CORDOVA_SERVICE_NAME, - "write", - [ this.socketKey, dataToWrite ]); - }; - - Socket.prototype.shutdownWrite = function (success, error) { - - success = success || function() { }; - error = error || function() { }; - - if (!this._ensureState(Socket.State.OPENED, error)) { - return; - } - - exec( - success, - error, - CORDOVA_SERVICE_NAME, - "shutdownWrite", - [ this.socketKey ]); - }; - - 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( - success, - error, - CORDOVA_SERVICE_NAME, - "close", - [ this.socketKey ]); - }; - - Object.defineProperty(Socket.prototype, "state", { - get: function () { - return this._state; + exec( + function () { + _that._state = Socket.State.OPENED; + window.document.addEventListener(SOCKET_EVENT, socketEventHandler); + success(); }, - enumerable: true, - configurable: true - }); + function (errorMessage) { + _that._state = Socket.State.CLOSED; + error(errorMessage); + }, + CORDOVA_SERVICE_NAME, + "open", + [ + this.socketKey, + host, + port + ]); +}; - 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; - } - }; +Socket.prototype.write = function (data, success, error) { - Socket.dispatchEvent = function (event) { - var eventReceive = document.createEvent('Events'); - eventReceive.initEvent(SOCKET_EVENT, true, true); - eventReceive.payload = event; - - 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 () { - function s4() { - return Math.floor((1 + Math.random()) * 0x10000) - .toString(16) - .substring(1); - } - - return function () { - return s4() + s4() + '-' + s4() + '-' + s4() + '-' + - s4() + '-' + s4() + s4() + s4(); + success = success || function () { }; - })(); + error = error || function () { + }; + + if (!this._ensureState(Socket.State.OPENED, error)) { + return; + } + + var dataToWrite = data instanceof Uint8Array + ? Socket._copyToArray(data) + : data; + + exec( + success, + error, + CORDOVA_SERVICE_NAME, + "write", + [ + this.socketKey, + dataToWrite + ]); +}; + +Socket.prototype.shutdownWrite = function (success, error) { + + success = success || function () { + }; + error = error || function () { + }; + + if (!this._ensureState(Socket.State.OPENED, error)) { + return; + } + + exec( + success, + error, + CORDOVA_SERVICE_NAME, + "shutdownWrite", + [this.socketKey]); +}; + +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( + success, + error, + CORDOVA_SERVICE_NAME, + "close", + [this.socketKey]); +}; + +Object.defineProperty(Socket.prototype, "state", { + get : function () { + return this._state; + }, + enumerable : true, + configurable : true +}); + +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; + } +}; + +Socket.dispatchEvent = function (event) { + var eventReceive = document.createEvent('Events'); + eventReceive.initEvent(SOCKET_EVENT, true, true); + eventReceive.payload = event; + + 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 () { + function s4() { + return Math.floor((1 + Math.random()) * 0x10000) + .toString(16) + .substring(1); + } + + return function () { + return s4() + s4() + '-' + s4() + '-' + s4() + '-' + + s4() + '-' + s4() + s4() + s4(); + }; +})(); // Register event dispatcher for Windows Phone - if (navigator.userAgent.match(/iemobile/i)) { - window.document.addEventListener("deviceready", function () { - exec( - Socket.dispatchEvent, - function (errorMessage) { - console.error("SocketsForCordova: Cannot register WP event dispatcher, Error: " + errorMessage); - }, - CORDOVA_SERVICE_NAME, - "registerWPEventDispatcher", - [ ]); - }); - } +if (navigator.userAgent.match(/iemobile/i)) { + window.document.addEventListener("deviceready", function () { + exec( + Socket.dispatchEvent, + function (errorMessage) { + console.error("SocketsForCordova: Cannot register WP event dispatcher, Error: " + errorMessage); + }, + CORDOVA_SERVICE_NAME, + "registerWPEventDispatcher", + []); + }); +} - module.exports = Socket; - -}); \ No newline at end of file +module.exports = Socket; \ No newline at end of file diff --git a/src/ios/.DS_Store b/src/ios/.DS_Store index ccc74a21330c6f94b4fb981d151192a4477d4662..a41a814ed55f8acec06ae5860f7579079ac79099 100644 GIT binary patch literal 10244 zcmeHMU2GIp6h3ELU}jp{DFQ8Bp_?`p)B7%?C?-A`V`4;66Rlb`B${YUeDg_t!gKGPZQ2F2AqEW2ZSFny z=bSV5eDmG8dv5^%JFGMpfIQL2va8+_2s0H!W>YT1Nu)!|Ad14>0}q- z>VQ!pgEBxMK;UWw*x7v%Bqbb zkjS~|gvIB6F_E@R*K2J2217-~3l=VtB&kGNT=H=5h?DE}G%xFRXqh42GIb;2*ZUmD znyG7v{iczNt4li_+tWMd;>CMG1Qvc73s*P+z$6XPc*9zDZ@1N6U!P%hdt<=Od#Zn>kKBJ|J; z`Ov5Hq0351xa&csd%#ROcCUHDh^yr;;m}3Ro{TX{QT8VtE1R<2xLT3atYp^GJfq#R zn6f+I8Dk!+9dMnD*U1&*s+x6;y$0!-qehIh$#e`*5-ihs*hfO0RSKxK$g=tf>65eNN3Byu#Sv;q;O$qmp zijdWZ{LU>~mGD5)Nu?+hw*njd?r3{BJj|Zlt-0Qwv|-b%K4Ooj=yuSo2H9AUb0_OrecAd<-AQC-6x; zg{SdFJcDQPRXmSx;{|*N-@}XeF@A!V@GJZdzZdfi<%zsowDToirrJE#FcH~b!=d48 z5xIKjE+uwLiTrITV{gThJC-h6RkvnsbKBPTnYUca2A8;voEQA%ATv?JBE60t~?M69#unU$+liI!*oob^8~0!jF>?qH;xNWhBW51s z%uM5PJb@>1f*AS~K8?@dv-q6P(3kLKd=1YP@Ubh;L?Q8w`SEeIH)C6leT1|bv%~+q zkHI<^47ddsS;6@~IRF3MJ@(B$|NjLyB>}ns delta 277 zcmZn(XfcprU|?W$DortDU=RQ@Ie-{Mvv5r;6q~50$jGrVU^g=($7UV@UPh@qNd-BX z#U%y?HyD|iSyJOO-d{Z&rB)Fhz`!k zFNX?naB^~R#tTSPR~wmH>L?hR7}n}2R9je@=qQ+3TGZAGOyT5^R5!NwPRg(8=0Pl3=o%nkWQ5QRf>0Vptp+j#8G;$|8Il>Y8B!Ta7>XzRiC1oZFVV)hn4N<|kQt&< ifE!4=f_%2I@H_KleicuUKN*-Heg*{t!{&INIm`eCnm025 diff --git a/src/ios/SocketsForCordova/Classes/SocketAdapter.m b/src/ios/SocketsForCordova/Classes/SocketAdapter.m index b14f1d1..fe63191 100644 --- a/src/ios/SocketsForCordova/Classes/SocketAdapter.m +++ b/src/ios/SocketsForCordova/Classes/SocketAdapter.m @@ -77,7 +77,8 @@ int writeTimeoutSeconds = 5.0; -(void)onOpenTimeout:(NSTimer *)timer { NSLog(@"[NATIVE] Open timeout: %d", openTimeoutSeconds); - self.errorEventHandler(@"Socket open timeout", @"openTimeout"); + //self.errorEventHandler(@"Socket open timeout", @"openTimeout"); + self.openErrorEventHandler(@"Socket open timeout"); openTimer = nil; }