Browse Source

Add IOS server push support #143 #144

Ben Hsieh 7 years ago
parent
commit
dbaeef14a3

+ 13
- 1
src/index.js View File

@@ -116,7 +116,7 @@ function fetch(...args:any):Promise {
116 116
   // create task ID for receiving progress event
117 117
   let taskId = getUUID()
118 118
   let options = this || {}
119
-  let subscription, subscriptionUpload, stateEvent
119
+  let subscription, subscriptionUpload, stateEvent, partEvent
120 120
   let respInfo = {}
121 121
 
122 122
   let promise = new Promise((resolve, reject) => {
@@ -143,6 +143,12 @@ function fetch(...args:any):Promise {
143 143
       }
144 144
     })
145 145
 
146
+    partEvent = emitter.addListener('RNFetchBlobServerPush', (e) => {
147
+      if(e.taskId === taskId && promise.onPartData) {
148
+        promise.onPartData(e.chunk)
149
+      }
150
+    })
151
+
146 152
     // When the request body comes from Blob polyfill, we should use special its ref
147 153
     // as the request body
148 154
     if( body instanceof Blob && body.isRNFetchBlobPolyfill) {
@@ -168,9 +174,11 @@ function fetch(...args:any):Promise {
168 174
       subscription.remove()
169 175
       subscriptionUpload.remove()
170 176
       stateEvent.remove()
177
+      partEvent.remove()
171 178
       delete promise['progress']
172 179
       delete promise['uploadProgress']
173 180
       delete promise['stateChange']
181
+      delete promise['part']
174 182
       delete promise['cancel']
175 183
       promise.cancel = () => {}
176 184
 
@@ -229,6 +237,10 @@ function fetch(...args:any):Promise {
229 237
     RNFetchBlob.enableUploadProgressReport(taskId, interval, count)
230 238
     return promise
231 239
   }
240
+  promise.part = (fn) => {
241
+    promise.onPartData = fn
242
+    return promise
243
+  }
232 244
   promise.stateChange = (fn) => {
233 245
     promise.onStateChange = fn
234 246
     return promise

+ 1
- 0
src/ios/RNFetchBlobConst.h View File

@@ -18,6 +18,7 @@ extern NSString *const MSG_EVENT_WARN;
18 18
 extern NSString *const MSG_EVENT_ERROR;
19 19
 
20 20
 extern NSString *const EVENT_PROGRESS;
21
+extern NSString *const EVENT_SERVER_PUSH;
21 22
 extern NSString *const EVENT_PROGRESS_UPLOAD;
22 23
 extern NSString *const EVENT_STATE_CHANGE;
23 24
 

+ 2
- 1
src/ios/RNFetchBlobConst.m View File

@@ -23,6 +23,7 @@ extern NSString *const CONFIG_KEY = @"key";
23 23
 extern NSString *const CONFIG_EXTRA_BLOB_CTYPE = @"binaryContentTypes";
24 24
 
25 25
 extern NSString *const EVENT_STATE_CHANGE = @"RNFetchBlobState";
26
+extern NSString *const EVENT_SERVER_PUSH = @"RNFetchBlobServerPush";
26 27
 extern NSString *const EVENT_PROGRESS = @"RNFetchBlobProgress";
27 28
 extern NSString *const EVENT_PROGRESS_UPLOAD = @"RNFetchBlobProgress-upload";
28 29
 
@@ -41,4 +42,4 @@ extern NSString *const KEY_REPORT_UPLOAD_PROGRESS = @"reportUploadProgress";
41 42
 // response type
42 43
 extern NSString *const RESP_TYPE_BASE64 = @"base64";
43 44
 extern NSString *const RESP_TYPE_UTF8 = @"utf8";
44
-extern NSString *const RESP_TYPE_PATH = @"path";
45
+extern NSString *const RESP_TYPE_PATH = @"path";

+ 1
- 0
src/ios/RNFetchBlobNetwork.h View File

@@ -22,6 +22,7 @@ typedef void(^DataTaskCompletionHander) (NSData * _Nullable resp, NSURLResponse
22 22
 @property (nullable, nonatomic) NSString * taskId;
23 23
 @property (nonatomic) int expectedBytes;
24 24
 @property (nonatomic) int receivedBytes;
25
+@property (nonatomic) BOOL isServerPush;
25 26
 @property (nullable, nonatomic) NSMutableData * respData;
26 27
 @property (strong, nonatomic) RCTResponseSenderBlock callback;
27 28
 @property (nullable, nonatomic) RCTBridge * bridge;

+ 31
- 0
src/ios/RNFetchBlobNetwork.m View File

@@ -38,6 +38,8 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
38 38
 @interface RNFetchBlobNetwork ()
39 39
 {
40 40
     BOOL * respFile;
41
+    BOOL isNewPart;
42
+    NSMutableData * partBuffer;
41 43
     NSString * destPath;
42 44
     NSOutputStream * writeStream;
43 45
     long bodyLength;
@@ -222,6 +224,26 @@ NSOperationQueue *taskQueue;
222 224
     {
223 225
         NSDictionary *headers = [httpResponse allHeaderFields];
224 226
         NSString * respCType = [[RNFetchBlobReqBuilder getHeaderIgnoreCases:@"Content-Type" fromHeaders:headers] lowercaseString];
227
+        if(self.isServerPush == NO)
228
+        {
229
+            self.isServerPush = [[respCType lowercaseString] RNFBContainsString:@"multipart/x-mixed-replace;"];
230
+        }
231
+        if(self.isServerPush)
232
+        {
233
+            if(partBuffer != nil)
234
+            {
235
+                [self.bridge.eventDispatcher
236
+                 sendDeviceEventWithName:EVENT_SERVER_PUSH
237
+                 body:@{
238
+                        @"taskId": taskId,
239
+                        @"chunk": [partBuffer base64EncodedStringWithOptions:0],
240
+                        }
241
+                 ];
242
+            }
243
+            partBuffer = [[NSMutableData alloc] init];
244
+            completionHandler(NSURLSessionResponseAllow);
245
+            return;
246
+        }
225 247
         if(respCType != nil)
226 248
         {
227 249
             NSArray * extraBlobCTypes = [options objectForKey:CONFIG_EXTRA_BLOB_CTYPE];
@@ -311,9 +333,17 @@ NSOperationQueue *taskQueue;
311 333
     completionHandler(NSURLSessionResponseAllow);
312 334
 }
313 335
 
336
+
314 337
 // download progress handler
315 338
 - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
316 339
 {
340
+    // For #143 handling multipart/x-mixed-replace response
341
+    if(self.isServerPush)
342
+    {
343
+        [partBuffer appendData:data];
344
+        return ;
345
+    }
346
+    
317 347
     NSNumber * received = [NSNumber numberWithLong:[data length]];
318 348
     receivedBytes += [received longValue];
319 349
     if(respFile == NO)
@@ -349,6 +379,7 @@ NSOperationQueue *taskQueue;
349 379
         session = nil;
350 380
 }
351 381
 
382
+
352 383
 - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
353 384
 {
354 385