Added network error codes support

This commit is contained in:
nikita.kosilo 2020-10-19 12:51:03 +03:00
parent 961a68ed13
commit 3de2552705
7 changed files with 41 additions and 18 deletions

View File

@ -165,3 +165,4 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- 1.5.3 - added Android open and write timeouts [Android] - 1.5.3 - added Android open and write timeouts [Android]
- 1.5.4 - fixed iOS closing sockets on open timeout [iOS] - 1.5.4 - fixed iOS closing sockets on open timeout [iOS]
- 1.6.0 - close old existing sockets on reopen by destination ports. Removed iOS trash sources [iOS, Android] - 1.6.0 - close old existing sockets on reopen by destination ports. Removed iOS trash sources [iOS, Android]
- 1.7.0 - added codes to error handlers [iOS, Android]

View File

@ -1,6 +1,6 @@
{ {
"name": "cordova-plugin-socket-tcp", "name": "cordova-plugin-socket-tcp",
"version": "1.6.1", "version": "1.7.0",
"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" xmlns:android="http://schemas.android.com/apk/res/android" id="cordova-plugin-socket-tcp" version="1.6.0"> <plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" id="cordova-plugin-socket-tcp" version="1.7.0">
<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.

View File

@ -259,6 +259,7 @@ public class SocketPlugin extends CordovaPlugin {
JSONObject event = new JSONObject(); JSONObject event = new JSONObject();
event.put("type", "Error"); event.put("type", "Error");
event.put("errorMessage", errorMessage); event.put("errorMessage", errorMessage);
event.put("code", 0);
event.put("socketKey", socketKey); event.put("socketKey", socketKey);
dispatchEvent(event); dispatchEvent(event);
@ -277,7 +278,11 @@ public class SocketPlugin extends CordovaPlugin {
@Override @Override
public void accept(String errorMessage) { public void accept(String errorMessage) {
this.openCallbackContext.error(errorMessage); JSONObject event = new JSONObject();
event.put("errorMessage", errorMessage);
event.put("socketKey", "key");
event.put("code", 0);
this.openCallbackContext.error(event);
} }
} }

View File

@ -36,9 +36,9 @@
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)event; - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)event;
@property (copy) void (^openEventHandler)(); @property (copy) void (^openEventHandler)();
@property (copy) void (^openErrorEventHandler)(NSString*); @property (copy) void (^openErrorEventHandler)(NSString*, NSInteger);
@property (copy) void (^dataConsumer)(NSArray*); @property (copy) void (^dataConsumer)(NSArray*);
@property (copy) void (^closeEventHandler)(BOOL); @property (copy) void (^closeEventHandler)(BOOL);
@property (copy) void (^errorEventHandler)(NSString*, NSString *); @property (copy) void (^errorEventHandler)(NSString*, NSString *, NSInteger);
@end @end

View File

@ -78,14 +78,14 @@ 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"); self.openErrorEventHandler(@"Socket open timeout", 0);
openTimer = nil; openTimer = nil;
[self close]; [self close];
} }
-(void)onWriteTimeout:(NSTimer *)timer { -(void)onWriteTimeout:(NSTimer *)timer {
NSLog(@"[NATIVE] Write timeout: %d", writeTimeoutSeconds); NSLog(@"[NATIVE] Write timeout: %d", writeTimeoutSeconds);
self.errorEventHandler(@"Socket write timeout", @"writeTimeout"); self.errorEventHandler(@"Socket write timeout", @"writeTimeout", 0);
writeTimer = nil; writeTimer = nil;
} }
@ -208,13 +208,23 @@ int writeTimeoutSeconds = 5.0;
{ {
NSLog(@"[NATIVE] Stream event error: %@", [[stream streamError] localizedDescription]); NSLog(@"[NATIVE] Stream event error: %@", [[stream streamError] localizedDescription]);
NSInteger code = [[stream streamError] code];
if (wasOpenned) { if (wasOpenned) {
self.errorEventHandler([[stream streamError] localizedDescription], @"general"); self.errorEventHandler([[stream streamError] localizedDescription], @"general", code);
self.openErrorEventHandler([[stream streamError] localizedDescription],
code);
if(openTimer != nil){
NSLog(@"[NATIVE] openTimer invalidate on open event");
[openTimer invalidate];
openTimer = nil;
}
self.closeEventHandler(TRUE); self.closeEventHandler(TRUE);
} }
else { else {
self.errorEventHandler([[stream streamError] localizedDescription], @"general"); self.errorEventHandler([[stream streamError] localizedDescription], @"general", code);
self.openErrorEventHandler([[stream streamError] localizedDescription]); self.openErrorEventHandler([[stream streamError] localizedDescription],
code);
} }
//[self closeStreams]; //[self closeStreams];
break; break;

View File

@ -26,7 +26,7 @@
NSString *socketKey = [command.arguments objectAtIndex:0]; NSString *socketKey = [command.arguments objectAtIndex:0];
NSString *host = [command.arguments objectAtIndex:1]; NSString *host = [command.arguments objectAtIndex:1];
NSNumber *port = [NSNumber numberWithInteger:[[command.arguments objectAtIndex:2] integerValue]]; NSNumber *port = [command.arguments objectAtIndex:2];
NSLog(@"[NATIVE] OPEN socket for port: %@", port); NSLog(@"[NATIVE] OPEN socket for port: %@", port);
@ -53,18 +53,25 @@
socketAdapter = nil; socketAdapter = nil;
}; };
socketAdapter.openErrorEventHandler = ^ void (NSString *error){ socketAdapter.openErrorEventHandler = ^ void (NSString *error, NSInteger code){
NSLog(@"[NATIVE] openErrorEventHandler"); NSLog(@"[NATIVE] openErrorEventHandler. Code %ld", code);
NSMutableDictionary *errorDictionaryData = [[NSMutableDictionary alloc] init];
[errorDictionaryData setObject:error forKey:@"message"];
[errorDictionaryData setObject:socketKey forKey:@"socketKey"];
[errorDictionaryData setObject:[NSString stringWithFormat: @"%ld", (long)code] forKey:@"code"];
[self.commandDelegate [self.commandDelegate
sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error] sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:errorDictionaryData]
callbackId:command.callbackId]; callbackId:command.callbackId];
}; };
socketAdapter.errorEventHandler = ^ void (NSString *error, NSString *errorType){ socketAdapter.errorEventHandler = ^ void (NSString *error, NSString *errorType, NSInteger code){
NSMutableDictionary *errorDictionaryData = [[NSMutableDictionary alloc] init]; NSMutableDictionary *errorDictionaryData = [[NSMutableDictionary alloc] init];
[errorDictionaryData setObject:@"Error" forKey:@"type"]; [errorDictionaryData setObject:@"Error" forKey:@"type"];
[errorDictionaryData setObject:errorType forKey:@"errorType"]; [errorDictionaryData setObject:errorType forKey:@"errorType"];
[errorDictionaryData setObject:error forKey:@"errorMessage"]; [errorDictionaryData setObject:error forKey:@"errorMessage"];
[errorDictionaryData setObject:socketKey forKey:@"socketKey"]; [errorDictionaryData setObject:socketKey forKey:@"socketKey"];
[errorDictionaryData setObject:[NSString stringWithFormat: @"%ld", (long)code] forKey:@"code"];
[self dispatchEventWithDictionary:errorDictionaryData]; [self dispatchEventWithDictionary:errorDictionaryData];
}; };