Add support for binary type data (ArrayBuffer)

Summary:
Entirely based on https://github.com/facebook/react-native/pull/1829
I just updated the diff to be compatible with the current master branch.
Closes https://github.com/facebook/react-native/pull/4483

Reviewed By: svcscm

Differential Revision: D2783425

Pulled By: nicklockwood

fb-gh-sync-id: 1cc67c0741cff3b071530877d688f6b8c1061e3f
This commit is contained in:
Johan Lindskogen
2015-12-22 09:22:01 -08:00
committed by facebook-github-bot-3
parent 6df737d1e7
commit afbff9bf88
4 changed files with 66 additions and 56 deletions

View File

@@ -76,8 +76,10 @@ RCT_EXPORT_METHOD(close:(nonnull NSNumber *)socketID)
- (void)webSocket:(RCTSRWebSocket *)webSocket didReceiveMessage:(id)message
{
BOOL binary = [message isKindOfClass:[NSData class]];
[_bridge.eventDispatcher sendDeviceEventWithName:@"websocketMessage" body:@{
@"data": message,
@"data": binary ? [message base64EncodedStringWithOptions:0] : message,
@"type": binary ? @"binary" : @"text",
@"id": webSocket.reactTag
}];
}

View File

@@ -17,6 +17,8 @@ var Platform = require('Platform');
var WebSocketBase = require('WebSocketBase');
var WebSocketEvent = require('WebSocketEvent');
var base64 = require('base64-js');
var WebSocketId = 0;
var CLOSE_NORMAL = 1000;
@@ -79,7 +81,7 @@ class WebSocket extends WebSocketBase {
return;
}
var event = new WebSocketEvent('message', {
data: ev.data
data: (ev.type === 'binary') ? base64.toByteArray(ev.data).buffer : ev.data
});
this.onmessage && this.onmessage(event);
this.dispatchEvent(event);