+ [ios] add logic to method

Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/ec94e28e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/ec94e28e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/ec94e28e

Branch: refs/heads/0.16-dev
Commit: ec94e28e545154d37759a8f8e5a888926762cff5
Parents: fd8f177
Author: 齐山 <sunjjb...@163.com>
Authored: Thu Aug 3 11:46:12 2017 +0800
Committer: 齐山 <sunjjb...@163.com>
Committed: Thu Aug 3 11:46:12 2017 +0800

----------------------------------------------------------------------
 examples/vue/modules/websocket.vue              |  8 +++----
 .../WeexSDK/Sources/Module/WXWebSocketModule.m  | 12 ++++-------
 ios/sdk/WeexSDK/Sources/Utility/WXUtility.h     | 12 +++++++++++
 ios/sdk/WeexSDK/Sources/Utility/WXUtility.m     | 22 ++++++++++++++++++++
 4 files changed, 42 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/ec94e28e/examples/vue/modules/websocket.vue
----------------------------------------------------------------------
diff --git a/examples/vue/modules/websocket.vue 
b/examples/vue/modules/websocket.vue
index e6ba2e1..e2ab2ce 100644
--- a/examples/vue/modules/websocket.vue
+++ b/examples/vue/modules/websocket.vue
@@ -88,11 +88,11 @@
           self.onopeninfo = 'websocket open';
         }
         websocket.onmessage = function(e) {
-          if(Object.prototype.toString.apply(e.data) === 'ArrayBuffer'){
-            self.onmessage = 'you receive array buffer'
-          }
-          if(Object.prototype.toString.apply(e.data) === 'String'){
+          if(typeof(e.data) === 'String'){
             self.onmessage = e.data;
+          }else
+          {
+            self.onmessage = 'you receive array buffer'
           }
 
         }

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/ec94e28e/ios/sdk/WeexSDK/Sources/Module/WXWebSocketModule.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Module/WXWebSocketModule.m 
b/ios/sdk/WeexSDK/Sources/Module/WXWebSocketModule.m
index 046702d..e12a11b 100644
--- a/ios/sdk/WeexSDK/Sources/Module/WXWebSocketModule.m
+++ b/ios/sdk/WeexSDK/Sources/Module/WXWebSocketModule.m
@@ -62,11 +62,7 @@ WX_EXPORT_METHOD(@selector(onclose:))
             if([message isKindOfClass:[NSString class]]) {
                 [dic setObject:message forKey:@"data"];
             }else if([message isKindOfClass:[NSData class]]){
-                NSMutableDictionary *dataDict = [NSMutableDictionary new];
-                NSString *base64Encoded = [message 
base64EncodedStringWithOptions:0];
-                [dataDict setObject:@"binary" forKey:@"@type"];
-                [dataDict setObject:base64Encoded forKey:@"base64"];
-                [dic setObject:dataDict forKey:@"data"];
+                [dic setObject:[WXUtility dataToBase64Dict:message] 
forKey:@"data"];
             }
             if (weakSelf.messageCallBack) {
                 weakSelf.messageCallBack(dic,true);;
@@ -114,11 +110,11 @@ WX_EXPORT_METHOD(@selector(onclose:))
     if([data isKindOfClass:[NSString class]]){
         [loader send:data];
     }else if([data isKindOfClass:[NSDictionary class]]){
-        if([@"binary" isEqualToString:data[@"@type"]]){
-            NSString *base64 = data[@"base64"];
-            NSData *sendData = [[NSData alloc] 
initWithBase64EncodedString:base64 options:0];
+        NSData *sendData = [WXUtility base64DictToData:data];
+        if(sendData){
             [loader send:sendData];
         }
+        
     }
     
     

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/ec94e28e/ios/sdk/WeexSDK/Sources/Utility/WXUtility.h
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Utility/WXUtility.h 
b/ios/sdk/WeexSDK/Sources/Utility/WXUtility.h
index 715617d..5d96757 100644
--- a/ios/sdk/WeexSDK/Sources/Utility/WXUtility.h
+++ b/ios/sdk/WeexSDK/Sources/Utility/WXUtility.h
@@ -439,4 +439,16 @@ BOOL WXFloatGreaterThanWithPrecision(CGFloat a,CGFloat 
b,double precision);
  */
 + (void)customMonitorInfo:(WXSDKInstance *_Nullable)instance key:(NSString * 
_Nonnull)key value:(id _Nonnull)value;
 
+/**
+ *  @abstract format to base64 dictionary
+ *
+ */
++ (NSDictionary *_Nonnull)dataToBase64Dict:(NSData *_Nullable)data;
+
+/**
+ *  @abstract format to data
+ *
+ */
++ (NSData *_Nonnull)base64DictToData:(NSDictionary *_Nullable)base64Dict;
+
 @end

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/ec94e28e/ios/sdk/WeexSDK/Sources/Utility/WXUtility.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Utility/WXUtility.m 
b/ios/sdk/WeexSDK/Sources/Utility/WXUtility.m
index 030ffc8..5487525 100644
--- a/ios/sdk/WeexSDK/Sources/Utility/WXUtility.m
+++ b/ios/sdk/WeexSDK/Sources/Utility/WXUtility.m
@@ -901,6 +901,28 @@ BOOL WXFloatGreaterThanWithPrecision(CGFloat a, CGFloat b 
,double precision){
     [custormMonitorDict setObject:value forKey:key];
     instance.userInfo[WXCUSTOMMONITORINFO] = custormMonitorDict;
 }
+
++ (NSDictionary *_Nonnull)dataToBase64Dict:(NSData *_Nullable)data
+{
+    NSMutableDictionary *dataDict = [NSMutableDictionary new];
+    if(data){
+        NSString *base64Encoded = [data base64EncodedStringWithOptions:0];
+        [dataDict setObject:@"binary" forKey:@"@type"];
+        [dataDict setObject:base64Encoded forKey:@"base64"];
+    }
+    
+    return dataDict;
+}
+
++ (NSData *_Nonnull)base64DictToData:(NSDictionary *_Nullable)base64Dict
+{
+    if([@"binary" isEqualToString:base64Dict[@"@type"]]){
+        NSString *base64 = base64Dict[@"base64"];
+        NSData *sendData = [[NSData alloc] initWithBase64EncodedString:base64 
options:0];
+        return sendData;
+    }
+    return nil;
+}
 @end
 
 

Reply via email to