Fixed cordova js bridge implementation, fixed iOS open timeout

This commit is contained in:
kitolog 2018-03-27 11:14:43 +03:00
parent ebe8bc7bd3
commit 26a14948e3
6 changed files with 217 additions and 206 deletions

View File

@ -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. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
## What's new ## What's new
1.2.3 - fixed ios socket closing crashes 1.2.3 - fixed iOS socket closing crashes
1.5.0 - added ios open and write timeouts, changed js errors format 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

View File

@ -1,6 +1,6 @@
{ {
"name": "cordova-plugin-socket-tcp", "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.", "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": { "cordova": {
"platforms": [ "platforms": [

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="cordova-plugin-socket-tcp" version="1.5.0"> <plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="cordova-plugin-socket-tcp" version="1.5.2">
<name>SocketsForCordova</name> <name>SocketsForCordova</name>
<description> <description>
This Cordova plugin provides JavaScript API, that allows you to communicate with server through TCP protocol. This Cordova plugin provides JavaScript API, that allows you to communicate with server through TCP protocol.
@ -49,7 +49,6 @@
<!--<framework src="CoreGraphics.framework" />--> <!--<framework src="CoreGraphics.framework" />-->
</platform> </platform>
<!-- wp8 --> <!-- wp8 -->
<platform name="wp8"> <platform name="wp8">
<config-file target="config.xml" parent="/*"> <config-file target="config.xml" parent="/*">
@ -65,7 +64,4 @@
<source-file src="src/wp8/src/SocketEvent.cs" target-dir="src" /> <source-file src="src/wp8/src/SocketEvent.cs" target-dir="src" />
<source-file src="src/wp8/src/SocketStorage.cs" target-dir="src" /> <source-file src="src/wp8/src/SocketStorage.cs" target-dir="src" />
</platform> </platform>
</plugin> </plugin>

406
socket.js
View File

@ -1,221 +1,233 @@
cordova.define("cordova-plugin-socket-tcp.Socket", function(require, exports, module) { /**
/** * Copyright (c) 2015, Blocshop s.r.o.
* Copyright (c) 2015, Blocshop s.r.o. * All rights reserved.
* All rights reserved. *
* * Redistribution and use in source and binary forms are permitted
* Redistribution and use in source and binary forms are permitted * provided that the above copyright notice and this paragraph are
* provided that the above copyright notice and this paragraph are * duplicated in all such forms and that any documentation,
* duplicated in all such forms and that any documentation, * advertising materials, and other materials related to such
* advertising materials, and other materials related to such * distribution and use acknowledge that the software was developed
* distribution and use acknowledge that the software was developed * by the Blocshop s.r.o.. The name of the
* by the Blocshop s.r.o.. The name of the * Blocshop s.r.o. may not be used to endorse or promote products derived
* Blocshop s.r.o. may not be used to endorse or promote products derived * from this software without specific prior written permission.
* from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* 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 SOCKET_EVENT = "SOCKET_EVENT";
var CORDOVA_SERVICE_NAME = "SocketsForCordova"; var CORDOVA_SERVICE_NAME = "SocketsForCordova";
Socket.State = {}; Socket.State = {};
Socket.State[Socket.State.CLOSED = 0] = "CLOSED"; Socket.State[Socket.State.CLOSED = 0] = "CLOSED";
Socket.State[Socket.State.OPENING = 1] = "OPENING"; Socket.State[Socket.State.OPENING = 1] = "OPENING";
Socket.State[Socket.State.OPENED = 2] = "OPENED"; Socket.State[Socket.State.OPENED = 2] = "OPENED";
Socket.State[Socket.State.CLOSING = 3] = "CLOSING"; Socket.State[Socket.State.CLOSING = 3] = "CLOSING";
Socket.ErrorType = {}; Socket.ErrorType = {};
Socket.ErrorType[Socket.ErrorType.GENERAL = 0] = "general"; Socket.ErrorType[Socket.ErrorType.GENERAL = 0] = "general";
Socket.ErrorType[Socket.ErrorType.OPEN_TIMEOUT = 1] = "openTimeout"; Socket.ErrorType[Socket.ErrorType.OPEN_TIMEOUT = 1] = "openTimeout";
Socket.ErrorType[Socket.ErrorType.WRITE_TIMEOUT = 2] = "writeTimeout"; Socket.ErrorType[Socket.ErrorType.WRITE_TIMEOUT = 2] = "writeTimeout";
function Socket() { function Socket() {
this._state = Socket.State.CLOSED; this._state = Socket.State.CLOSED;
this.onData = null; this.onData = null;
this.onClose = null; this.onClose = null;
this.onError = null; this.onError = null;
this.socketKey = guid(); 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() { }; function socketEventHandler(event) {
error = error || function() { };
if (!this._ensureState(Socket.State.CLOSED, error)) { var payload = event.payload;
if (payload.socketKey !== _that.socketKey) {
return; return;
} }
var _that = this; switch (payload.type) {
case "Close":
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) {
_that._state = Socket.State.CLOSED; _that._state = Socket.State.CLOSED;
error(errorMessage); window.document.removeEventListener(SOCKET_EVENT, socketEventHandler);
}, _that.onClose(payload.hasError);
CORDOVA_SERVICE_NAME, break;
"open", case "DataReceived":
[ this.socketKey, host, port ]); _that.onData(new Uint8Array(payload.data));
}; break;
case "Error":
Socket.prototype.write = function (data, success, error) { _that.onError(payload);
break;
success = success || function() { }; default:
error = error || function() { }; console.error("SocketsForCordova: Unknown event type " + payload.type + ", socket key: " + payload.socketKey);
break;
if (!this._ensureState(Socket.State.OPENED, error)) {
return;
} }
}
var dataToWrite = data instanceof Uint8Array _that._state = Socket.State.OPENING;
? Socket._copyToArray(data)
: data;
exec( exec(
success, function () {
error, _that._state = Socket.State.OPENED;
CORDOVA_SERVICE_NAME, window.document.addEventListener(SOCKET_EVENT, socketEventHandler);
"write", success();
[ 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, function (errorMessage) {
configurable: true _that._state = Socket.State.CLOSED;
}); error(errorMessage);
},
CORDOVA_SERVICE_NAME,
"open",
[
this.socketKey,
host,
port
]);
};
Socket.prototype._ensureState = function(requiredState, errorCallback) { Socket.prototype.write = function (data, success, error) {
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) { success = success || function () {
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();
}; };
})(); 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 // 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,
"registerWPEventDispatcher", "registerWPEventDispatcher",
[ ]); []);
}); });
} }
module.exports = Socket; module.exports = Socket;
});

BIN
src/ios/.DS_Store vendored

Binary file not shown.

View File

@ -77,7 +77,8 @@ int writeTimeoutSeconds = 5.0;
-(void)onOpenTimeout:(NSTimer *)timer { -(void)onOpenTimeout:(NSTimer *)timer {
NSLog(@"[NATIVE] Open timeout: %d", openTimeoutSeconds); 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; openTimer = nil;
} }