From 3de2552705e81b99f72bd57d094b5db4db40bafb Mon Sep 17 00:00:00 2001 From: "nikita.kosilo" Date: Mon, 19 Oct 2020 12:51:03 +0300 Subject: [PATCH] Added network error codes support --- README.md | 3 ++- package.json | 2 +- plugin.xml | 2 +- .../socketsforcordova/SocketPlugin.java | 7 +++++- .../SocketsForCordova/Classes/SocketAdapter.h | 6 ++--- .../SocketsForCordova/Classes/SocketAdapter.m | 22 ++++++++++++++----- .../SocketsForCordova/Classes/SocketPlugin.m | 17 +++++++++----- 7 files changed, 41 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index a5b9b23..ac72e23 100644 --- a/README.md +++ b/README.md @@ -164,4 +164,5 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - 1.5.2 - fixed iOS open timeout [iOS] - 1.5.3 - added Android open and write timeouts [Android] - 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] \ No newline at end of file + - 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] \ No newline at end of file diff --git a/package.json b/package.json index 4cb72bf..6c8199d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "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.", "cordova": { "platforms": [ diff --git a/plugin.xml b/plugin.xml index 585fe79..8a07e45 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. diff --git a/src/android/src/cz/blocshop/socketsforcordova/SocketPlugin.java b/src/android/src/cz/blocshop/socketsforcordova/SocketPlugin.java index 9939ab9..f94550b 100644 --- a/src/android/src/cz/blocshop/socketsforcordova/SocketPlugin.java +++ b/src/android/src/cz/blocshop/socketsforcordova/SocketPlugin.java @@ -259,6 +259,7 @@ public class SocketPlugin extends CordovaPlugin { JSONObject event = new JSONObject(); event.put("type", "Error"); event.put("errorMessage", errorMessage); + event.put("code", 0); event.put("socketKey", socketKey); dispatchEvent(event); @@ -277,7 +278,11 @@ public class SocketPlugin extends CordovaPlugin { @Override 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); } } diff --git a/src/ios/SocketsForCordova/Classes/SocketAdapter.h b/src/ios/SocketsForCordova/Classes/SocketAdapter.h index c609205..5adc1b2 100644 --- a/src/ios/SocketsForCordova/Classes/SocketAdapter.h +++ b/src/ios/SocketsForCordova/Classes/SocketAdapter.h @@ -36,9 +36,9 @@ - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)event; @property (copy) void (^openEventHandler)(); -@property (copy) void (^openErrorEventHandler)(NSString*); +@property (copy) void (^openErrorEventHandler)(NSString*, NSInteger); @property (copy) void (^dataConsumer)(NSArray*); @property (copy) void (^closeEventHandler)(BOOL); -@property (copy) void (^errorEventHandler)(NSString*, NSString *); +@property (copy) void (^errorEventHandler)(NSString*, NSString *, NSInteger); -@end \ No newline at end of file +@end diff --git a/src/ios/SocketsForCordova/Classes/SocketAdapter.m b/src/ios/SocketsForCordova/Classes/SocketAdapter.m index 8922205..ac2c97b 100644 --- a/src/ios/SocketsForCordova/Classes/SocketAdapter.m +++ b/src/ios/SocketsForCordova/Classes/SocketAdapter.m @@ -78,14 +78,14 @@ int writeTimeoutSeconds = 5.0; -(void)onOpenTimeout:(NSTimer *)timer { NSLog(@"[NATIVE] Open timeout: %d", openTimeoutSeconds); //self.errorEventHandler(@"Socket open timeout", @"openTimeout"); - self.openErrorEventHandler(@"Socket open timeout"); + self.openErrorEventHandler(@"Socket open timeout", 0); openTimer = nil; [self close]; } -(void)onWriteTimeout:(NSTimer *)timer { NSLog(@"[NATIVE] Write timeout: %d", writeTimeoutSeconds); - self.errorEventHandler(@"Socket write timeout", @"writeTimeout"); + self.errorEventHandler(@"Socket write timeout", @"writeTimeout", 0); writeTimer = nil; } @@ -208,13 +208,23 @@ int writeTimeoutSeconds = 5.0; { NSLog(@"[NATIVE] Stream event error: %@", [[stream streamError] localizedDescription]); + NSInteger code = [[stream streamError] code]; + 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); } else { - self.errorEventHandler([[stream streamError] localizedDescription], @"general"); - self.openErrorEventHandler([[stream streamError] localizedDescription]); + self.errorEventHandler([[stream streamError] localizedDescription], @"general", code); + self.openErrorEventHandler([[stream streamError] localizedDescription], + code); } //[self closeStreams]; break; @@ -277,4 +287,4 @@ int writeTimeoutSeconds = 5.0; } } -@end \ No newline at end of file +@end diff --git a/src/ios/SocketsForCordova/Classes/SocketPlugin.m b/src/ios/SocketsForCordova/Classes/SocketPlugin.m index 9f7c844..5837d3e 100644 --- a/src/ios/SocketsForCordova/Classes/SocketPlugin.m +++ b/src/ios/SocketsForCordova/Classes/SocketPlugin.m @@ -26,7 +26,7 @@ NSString *socketKey = [command.arguments objectAtIndex:0]; 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); @@ -53,18 +53,25 @@ socketAdapter = nil; }; - socketAdapter.openErrorEventHandler = ^ void (NSString *error){ - NSLog(@"[NATIVE] openErrorEventHandler"); + socketAdapter.openErrorEventHandler = ^ void (NSString *error, NSInteger code){ + 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 - sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error] + sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:errorDictionaryData] callbackId:command.callbackId]; }; - socketAdapter.errorEventHandler = ^ void (NSString *error, NSString *errorType){ + socketAdapter.errorEventHandler = ^ void (NSString *error, NSString *errorType, NSInteger code){ NSMutableDictionary *errorDictionaryData = [[NSMutableDictionary alloc] init]; [errorDictionaryData setObject:@"Error" forKey:@"type"]; [errorDictionaryData setObject:errorType forKey:@"errorType"]; [errorDictionaryData setObject:error forKey:@"errorMessage"]; [errorDictionaryData setObject:socketKey forKey:@"socketKey"]; + [errorDictionaryData setObject:[NSString stringWithFormat: @"%ld", (long)code] forKey:@"code"]; [self dispatchEventWithDictionary:errorDictionaryData]; };