mirror of
https://github.com/status-im/react-native.git
synced 2025-01-17 04:50:59 +00:00
ed903099b4
Summary: This is the first PR from a series of PRs grabbou and me will make to add blob support to React Native. The next PR will include blob support for XMLHttpRequest. I'd like to get this merged with minimal changes to preserve the attribution. My next PR can contain bigger changes. Blobs are used to transfer binary data between server and client. Currently React Native lacks a way to deal with binary data. The only thing that comes close is uploading files through a URI. Current workarounds to transfer binary data includes encoding and decoding them to base64 and and transferring them as string, which is not ideal, since it increases the payload size and the whole payload needs to be sent via the bridge every time changes are made. The PR adds a way to deal with blobs via a new native module. The blob is constructed on the native side and the data never needs to pass through the bridge. Currently the only way to create a blob is to receive a blob from the server via websocket. The PR is largely a direct port of https://github.com/silklabs/silk/tree/master/react-native-blobs by philikon into RN (with changes to integrate with RN), and attributed as such. > **Note:** This is a breaking change for all people running iOS without CocoaPods. You will have to manually add `RCTBlob.xcodeproj` to your `Libraries` and then, add it to Build Phases. Just follow the process of manual linking. We'll also need to document this process in the release notes. Related discussion - https://github.com/facebook/react-native/issues/11103 - `Image` can't show image when `URL.createObjectURL` is used with large images on Android The websocket integration can be tested via a simple server, ```js const fs = require('fs'); const http = require('http'); const WebSocketServer = require('ws').Server; const wss = new WebSocketServer({ server: http.createServer().listen(7232), }); wss.on('connection', (ws) => { ws.on('message', (d) => { console.log(d); }); ws.send(fs.readFileSync('./some-file')); }); ``` Then on the client, ```js var ws = new WebSocket('ws://localhost:7232'); ws.binaryType = 'blob'; ws.onerror = (error) => { console.error(error); }; ws.onmessage = (e) => { console.log(e.data); ws.send(e.data); }; ``` cc brentvatne ide Closes https://github.com/facebook/react-native/pull/11417 Reviewed By: sahrens Differential Revision: D5188484 Pulled By: javache fbshipit-source-id: 6afcbc4d19aa7a27b0dc9d52701ba400e7d7e98f
197 lines
5.1 KiB
Objective-C
197 lines
5.1 KiB
Objective-C
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*/
|
|
|
|
#import "RCTWebSocketModule.h"
|
|
|
|
#import <objc/runtime.h>
|
|
|
|
#import <React/RCTConvert.h>
|
|
#import <React/RCTUtils.h>
|
|
|
|
#import "RCTSRWebSocket.h"
|
|
|
|
@implementation RCTSRWebSocket (React)
|
|
|
|
- (NSNumber *)reactTag
|
|
{
|
|
return objc_getAssociatedObject(self, _cmd);
|
|
}
|
|
|
|
- (void)setReactTag:(NSNumber *)reactTag
|
|
{
|
|
objc_setAssociatedObject(self, @selector(reactTag), reactTag, OBJC_ASSOCIATION_COPY_NONATOMIC);
|
|
}
|
|
|
|
@end
|
|
|
|
@interface RCTWebSocketModule () <RCTSRWebSocketDelegate>
|
|
|
|
@end
|
|
|
|
@implementation RCTWebSocketModule
|
|
{
|
|
NSMutableDictionary<NSNumber *, RCTSRWebSocket *> *_sockets;
|
|
NSMutableDictionary<NSNumber *, id> *_contentHandlers;
|
|
}
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
// Used by RCTBlobModule
|
|
@synthesize methodQueue = _methodQueue;
|
|
|
|
- (NSArray *)supportedEvents
|
|
{
|
|
return @[@"websocketMessage",
|
|
@"websocketOpen",
|
|
@"websocketFailed",
|
|
@"websocketClosed"];
|
|
}
|
|
|
|
- (void)dealloc
|
|
{
|
|
for (RCTSRWebSocket *socket in _sockets.allValues) {
|
|
socket.delegate = nil;
|
|
[socket close];
|
|
}
|
|
}
|
|
|
|
RCT_EXPORT_METHOD(connect:(NSURL *)URL protocols:(NSArray *)protocols headers:(NSDictionary *)headers socketID:(nonnull NSNumber *)socketID)
|
|
{
|
|
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
|
|
|
|
// We load cookies from sharedHTTPCookieStorage (shared with XHR and
|
|
// fetch). To get secure cookies for wss URLs, replace wss with https
|
|
// in the URL.
|
|
NSURLComponents *components = [NSURLComponents componentsWithURL:URL resolvingAgainstBaseURL:true];
|
|
if ([components.scheme.lowercaseString isEqualToString:@"wss"]) {
|
|
components.scheme = @"https";
|
|
}
|
|
|
|
// Load and set the cookie header.
|
|
NSArray<NSHTTPCookie *> *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:components.URL];
|
|
request.allHTTPHeaderFields = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
|
|
|
|
// Load supplied headers
|
|
[headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) {
|
|
[request addValue:[RCTConvert NSString:value] forHTTPHeaderField:key];
|
|
}];
|
|
|
|
RCTSRWebSocket *webSocket = [[RCTSRWebSocket alloc] initWithURLRequest:request protocols:protocols];
|
|
webSocket.delegate = self;
|
|
webSocket.reactTag = socketID;
|
|
if (!_sockets) {
|
|
_sockets = [NSMutableDictionary new];
|
|
}
|
|
_sockets[socketID] = webSocket;
|
|
[webSocket open];
|
|
}
|
|
|
|
RCT_EXPORT_METHOD(send:(NSString *)message forSocketID:(nonnull NSNumber *)socketID)
|
|
{
|
|
[_sockets[socketID] send:message];
|
|
}
|
|
|
|
RCT_EXPORT_METHOD(sendBinary:(NSString *)base64String forSocketID:(nonnull NSNumber *)socketID)
|
|
{
|
|
[self sendData:[[NSData alloc] initWithBase64EncodedString:base64String options:0] forSocketID:socketID];
|
|
}
|
|
|
|
- (void)sendData:(NSData *)data forSocketID:(nonnull NSNumber *)socketID
|
|
{
|
|
[_sockets[socketID] send:data];
|
|
}
|
|
|
|
RCT_EXPORT_METHOD(ping:(nonnull NSNumber *)socketID)
|
|
{
|
|
[_sockets[socketID] sendPing:NULL];
|
|
}
|
|
|
|
RCT_EXPORT_METHOD(close:(nonnull NSNumber *)socketID)
|
|
{
|
|
[_sockets[socketID] close];
|
|
[_sockets removeObjectForKey:socketID];
|
|
}
|
|
|
|
- (void)setContentHandler:(id<RCTWebSocketContentHandler>)handler forSocketID:(NSString *)socketID
|
|
{
|
|
if (!_contentHandlers) {
|
|
_contentHandlers = [NSMutableDictionary new];
|
|
}
|
|
_contentHandlers[socketID] = handler;
|
|
}
|
|
|
|
#pragma mark - RCTSRWebSocketDelegate methods
|
|
|
|
- (void)webSocket:(RCTSRWebSocket *)webSocket didReceiveMessage:(id)message
|
|
{
|
|
NSString *type;
|
|
|
|
NSNumber *socketID = [webSocket reactTag];
|
|
id contentHandler = _contentHandlers[socketID];
|
|
if (contentHandler) {
|
|
message = [contentHandler processMessage:message forSocketID:socketID withType:&type];
|
|
} else {
|
|
if ([message isKindOfClass:[NSData class]]) {
|
|
type = @"binary";
|
|
message = [message base64EncodedStringWithOptions:0];
|
|
} else {
|
|
type = @"text";
|
|
}
|
|
}
|
|
|
|
[self sendEventWithName:@"websocketMessage" body:@{
|
|
@"data": message,
|
|
@"type": type,
|
|
@"id": webSocket.reactTag
|
|
}];
|
|
}
|
|
|
|
- (void)webSocketDidOpen:(RCTSRWebSocket *)webSocket
|
|
{
|
|
[self sendEventWithName:@"websocketOpen" body:@{
|
|
@"id": webSocket.reactTag
|
|
}];
|
|
}
|
|
|
|
- (void)webSocket:(RCTSRWebSocket *)webSocket didFailWithError:(NSError *)error
|
|
{
|
|
NSNumber *socketID = [webSocket reactTag];
|
|
_contentHandlers[socketID] = nil;
|
|
[self sendEventWithName:@"websocketFailed" body:@{
|
|
@"message": error.localizedDescription,
|
|
@"id": socketID
|
|
}];
|
|
}
|
|
|
|
- (void)webSocket:(RCTSRWebSocket *)webSocket
|
|
didCloseWithCode:(NSInteger)code
|
|
reason:(NSString *)reason
|
|
wasClean:(BOOL)wasClean
|
|
{
|
|
NSNumber *socketID = [webSocket reactTag];
|
|
_contentHandlers[socketID] = nil;
|
|
[self sendEventWithName:@"websocketClosed" body:@{
|
|
@"code": @(code),
|
|
@"reason": RCTNullIfNil(reason),
|
|
@"clean": @(wasClean),
|
|
@"id": socketID
|
|
}];
|
|
}
|
|
|
|
@end
|
|
|
|
@implementation RCTBridge (RCTWebSocketModule)
|
|
|
|
- (RCTWebSocketModule *)webSocketModule
|
|
{
|
|
return [self moduleForClass:[RCTWebSocketModule class]];
|
|
}
|
|
|
|
@end
|