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 - (void)webSocket:(RCTSRWebSocket *)webSocket didReceiveMessage:(id)message
{ {
BOOL binary = [message isKindOfClass:[NSData class]];
[_bridge.eventDispatcher sendDeviceEventWithName:@"websocketMessage" body:@{ [_bridge.eventDispatcher sendDeviceEventWithName:@"websocketMessage" body:@{
@"data": message, @"data": binary ? [message base64EncodedStringWithOptions:0] : message,
@"type": binary ? @"binary" : @"text",
@"id": webSocket.reactTag @"id": webSocket.reactTag
}]; }];
} }

View File

@ -17,6 +17,8 @@ var Platform = require('Platform');
var WebSocketBase = require('WebSocketBase'); var WebSocketBase = require('WebSocketBase');
var WebSocketEvent = require('WebSocketEvent'); var WebSocketEvent = require('WebSocketEvent');
var base64 = require('base64-js');
var WebSocketId = 0; var WebSocketId = 0;
var CLOSE_NORMAL = 1000; var CLOSE_NORMAL = 1000;
@ -79,7 +81,7 @@ class WebSocket extends WebSocketBase {
return; return;
} }
var event = new WebSocketEvent('message', { 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.onmessage && this.onmessage(event);
this.dispatchEvent(event); this.dispatchEvent(event);

113
npm-shrinkwrap.json generated
View File

@ -151,16 +151,16 @@
"from": "globals@>=6.4.0 <7.0.0", "from": "globals@>=6.4.0 <7.0.0",
"resolved": "https://registry.npmjs.org/globals/-/globals-6.4.1.tgz" "resolved": "https://registry.npmjs.org/globals/-/globals-6.4.1.tgz"
}, },
"recast": {
"version": "0.10.33",
"from": "recast@0.10.33",
"resolved": "https://registry.npmjs.org/recast/-/recast-0.10.33.tgz"
},
"js-tokens": { "js-tokens": {
"version": "1.0.1", "version": "1.0.1",
"from": "js-tokens@1.0.1", "from": "js-tokens@1.0.1",
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-1.0.1.tgz" "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-1.0.1.tgz"
}, },
"recast": {
"version": "0.10.33",
"from": "recast@0.10.33",
"resolved": "https://registry.npmjs.org/recast/-/recast-0.10.33.tgz"
},
"regenerator": { "regenerator": {
"version": "0.8.40", "version": "0.8.40",
"from": "regenerator@0.8.40", "from": "regenerator@0.8.40",
@ -284,16 +284,16 @@
"from": "globals@>=6.4.0 <7.0.0", "from": "globals@>=6.4.0 <7.0.0",
"resolved": "https://registry.npmjs.org/globals/-/globals-6.4.1.tgz" "resolved": "https://registry.npmjs.org/globals/-/globals-6.4.1.tgz"
}, },
"js-tokens": {
"version": "1.0.1",
"from": "js-tokens@1.0.1",
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-1.0.1.tgz"
},
"recast": { "recast": {
"version": "0.10.33", "version": "0.10.33",
"from": "recast@0.10.33", "from": "recast@0.10.33",
"resolved": "https://registry.npmjs.org/recast/-/recast-0.10.33.tgz" "resolved": "https://registry.npmjs.org/recast/-/recast-0.10.33.tgz"
}, },
"js-tokens": {
"version": "1.0.1",
"from": "js-tokens@1.0.1",
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-1.0.1.tgz"
},
"regenerator": { "regenerator": {
"version": "0.8.40", "version": "0.8.40",
"from": "regenerator@0.8.40", "from": "regenerator@0.8.40",
@ -663,6 +663,11 @@
"from": "balanced-match@>=0.2.0 <0.3.0", "from": "balanced-match@>=0.2.0 <0.3.0",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-0.2.1.tgz" "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-0.2.1.tgz"
}, },
"base64-js": {
"version": "0.0.8",
"from": "base64-js@0.0.8",
"resolved": "https://registry.npmjs.org/base64-js/-/base64-js-0.0.8.tgz"
},
"beeper": { "beeper": {
"version": "1.1.0", "version": "1.1.0",
"from": "beeper@>=1.0.0 <2.0.0", "from": "beeper@>=1.0.0 <2.0.0",
@ -1855,16 +1860,16 @@
"from": "asn1@0.1.11", "from": "asn1@0.1.11",
"resolved": "https://registry.npmjs.org/asn1/-/asn1-0.1.11.tgz" "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.1.11.tgz"
}, },
"async": {
"version": "1.5.0",
"from": "async@^1.4.0",
"resolved": "https://registry.npmjs.org/async/-/async-1.5.0.tgz"
},
"assert-plus": { "assert-plus": {
"version": "0.1.5", "version": "0.1.5",
"from": "assert-plus@^0.1.5", "from": "assert-plus@^0.1.5",
"resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-0.1.5.tgz" "resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-0.1.5.tgz"
}, },
"async": {
"version": "1.5.0",
"from": "async@^1.4.0",
"resolved": "https://registry.npmjs.org/async/-/async-1.5.0.tgz"
},
"aws-sign2": { "aws-sign2": {
"version": "0.6.0", "version": "0.6.0",
"from": "aws-sign2@~0.6.0", "from": "aws-sign2@~0.6.0",
@ -1880,16 +1885,16 @@
"from": "block-stream@*", "from": "block-stream@*",
"resolved": "https://registry.npmjs.org/block-stream/-/block-stream-0.0.8.tgz" "resolved": "https://registry.npmjs.org/block-stream/-/block-stream-0.0.8.tgz"
}, },
"brace-expansion": {
"version": "1.1.1",
"from": "brace-expansion@>=1.0.0 <2.0.0",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.1.tgz"
},
"boom": { "boom": {
"version": "2.10.1", "version": "2.10.1",
"from": "boom@^2.8.x", "from": "boom@^2.8.x",
"resolved": "https://registry.npmjs.org/boom/-/boom-2.10.1.tgz" "resolved": "https://registry.npmjs.org/boom/-/boom-2.10.1.tgz"
}, },
"brace-expansion": {
"version": "1.1.1",
"from": "brace-expansion@>=1.0.0 <2.0.0",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.1.tgz"
},
"caseless": { "caseless": {
"version": "0.11.0", "version": "0.11.0",
"from": "caseless@~0.11.0", "from": "caseless@~0.11.0",
@ -1945,16 +1950,16 @@
"from": "delayed-stream@~1.0.0", "from": "delayed-stream@~1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz" "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz"
}, },
"escape-string-regexp": {
"version": "1.0.3",
"from": "escape-string-regexp@^1.0.2",
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.3.tgz"
},
"delegates": { "delegates": {
"version": "0.1.0", "version": "0.1.0",
"from": "delegates@^0.1.0", "from": "delegates@^0.1.0",
"resolved": "https://registry.npmjs.org/delegates/-/delegates-0.1.0.tgz" "resolved": "https://registry.npmjs.org/delegates/-/delegates-0.1.0.tgz"
}, },
"escape-string-regexp": {
"version": "1.0.3",
"from": "escape-string-regexp@^1.0.2",
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.3.tgz"
},
"extend": { "extend": {
"version": "3.0.0", "version": "3.0.0",
"from": "extend@~3.0.0", "from": "extend@~3.0.0",
@ -2010,16 +2015,16 @@
"from": "has-ansi@^2.0.0", "from": "has-ansi@^2.0.0",
"resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz" "resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz"
}, },
"hawk": {
"version": "3.1.0",
"from": "hawk@~3.1.0",
"resolved": "https://registry.npmjs.org/hawk/-/hawk-3.1.0.tgz"
},
"has-unicode": { "has-unicode": {
"version": "1.0.1", "version": "1.0.1",
"from": "has-unicode@^1.0.0", "from": "has-unicode@^1.0.0",
"resolved": "https://registry.npmjs.org/has-unicode/-/has-unicode-1.0.1.tgz" "resolved": "https://registry.npmjs.org/has-unicode/-/has-unicode-1.0.1.tgz"
}, },
"hawk": {
"version": "3.1.0",
"from": "hawk@~3.1.0",
"resolved": "https://registry.npmjs.org/hawk/-/hawk-3.1.0.tgz"
},
"hoek": { "hoek": {
"version": "2.16.3", "version": "2.16.3",
"from": "hoek@2.x.x", "from": "hoek@2.x.x",
@ -2030,16 +2035,16 @@
"from": "http-signature@~0.11.0", "from": "http-signature@~0.11.0",
"resolved": "https://registry.npmjs.org/http-signature/-/http-signature-0.11.0.tgz" "resolved": "https://registry.npmjs.org/http-signature/-/http-signature-0.11.0.tgz"
}, },
"ini": {
"version": "1.3.4",
"from": "ini@~1.3.0",
"resolved": "https://registry.npmjs.org/ini/-/ini-1.3.4.tgz"
},
"inherits": { "inherits": {
"version": "2.0.1", "version": "2.0.1",
"from": "inherits@*", "from": "inherits@*",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.1.tgz" "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.1.tgz"
}, },
"ini": {
"version": "1.3.4",
"from": "ini@~1.3.0",
"resolved": "https://registry.npmjs.org/ini/-/ini-1.3.4.tgz"
},
"is-my-json-valid": { "is-my-json-valid": {
"version": "2.12.2", "version": "2.12.2",
"from": "is-my-json-valid@^2.12.2", "from": "is-my-json-valid@^2.12.2",
@ -2150,41 +2155,41 @@
"from": "pinkie@^1.0.0", "from": "pinkie@^1.0.0",
"resolved": "https://registry.npmjs.org/pinkie/-/pinkie-1.0.0.tgz" "resolved": "https://registry.npmjs.org/pinkie/-/pinkie-1.0.0.tgz"
}, },
"qs": {
"version": "5.2.0",
"from": "qs@~5.2.0",
"resolved": "https://registry.npmjs.org/qs/-/qs-5.2.0.tgz"
},
"pinkie-promise": { "pinkie-promise": {
"version": "1.0.0", "version": "1.0.0",
"from": "pinkie-promise@^1.0.0", "from": "pinkie-promise@^1.0.0",
"resolved": "https://registry.npmjs.org/pinkie-promise/-/pinkie-promise-1.0.0.tgz" "resolved": "https://registry.npmjs.org/pinkie-promise/-/pinkie-promise-1.0.0.tgz"
}, },
"request": { "qs": {
"version": "2.65.0", "version": "5.2.0",
"from": "request@2.x", "from": "qs@~5.2.0",
"resolved": "https://registry.npmjs.org/request/-/request-2.65.0.tgz" "resolved": "https://registry.npmjs.org/qs/-/qs-5.2.0.tgz"
}, },
"readable-stream": { "readable-stream": {
"version": "1.1.13", "version": "1.1.13",
"from": "readable-stream@^1.1.13", "from": "readable-stream@^1.1.13",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.13.tgz" "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.13.tgz"
}, },
"request": {
"version": "2.65.0",
"from": "request@2.x",
"resolved": "https://registry.npmjs.org/request/-/request-2.65.0.tgz"
},
"semver": { "semver": {
"version": "5.0.3", "version": "5.0.3",
"from": "semver@~5.0.1", "from": "semver@~5.0.1",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.0.3.tgz" "resolved": "https://registry.npmjs.org/semver/-/semver-5.0.3.tgz"
}, },
"string_decoder": {
"version": "0.10.31",
"from": "string_decoder@~0.10.x",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz"
},
"sntp": { "sntp": {
"version": "1.0.9", "version": "1.0.9",
"from": "sntp@1.x.x", "from": "sntp@1.x.x",
"resolved": "https://registry.npmjs.org/sntp/-/sntp-1.0.9.tgz" "resolved": "https://registry.npmjs.org/sntp/-/sntp-1.0.9.tgz"
}, },
"string_decoder": {
"version": "0.10.31",
"from": "string_decoder@~0.10.x",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz"
},
"stringstream": { "stringstream": {
"version": "0.0.5", "version": "0.0.5",
"from": "stringstream@~0.0.4", "from": "stringstream@~0.0.4",
@ -2210,16 +2215,16 @@
"from": "tar@~2.2.0", "from": "tar@~2.2.0",
"resolved": "https://registry.npmjs.org/tar/-/tar-2.2.1.tgz" "resolved": "https://registry.npmjs.org/tar/-/tar-2.2.1.tgz"
}, },
"tunnel-agent": {
"version": "0.4.1",
"from": "tunnel-agent@~0.4.1",
"resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.4.1.tgz"
},
"tough-cookie": { "tough-cookie": {
"version": "2.2.0", "version": "2.2.0",
"from": "tough-cookie@~2.2.0", "from": "tough-cookie@~2.2.0",
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.2.0.tgz" "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.2.0.tgz"
}, },
"tunnel-agent": {
"version": "0.4.1",
"from": "tunnel-agent@~0.4.1",
"resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.4.1.tgz"
},
"uid-number": { "uid-number": {
"version": "0.0.3", "version": "0.0.3",
"from": "uid-number@0.0.3", "from": "uid-number@0.0.3",

View File

@ -84,6 +84,7 @@
"babel-polyfill": "^6.0.16", "babel-polyfill": "^6.0.16",
"babel-types": "^6.1.2", "babel-types": "^6.1.2",
"babylon": "^6.1.2", "babylon": "^6.1.2",
"base64-js": "^0.0.8",
"bser": "^1.0.2", "bser": "^1.0.2",
"chalk": "^1.1.1", "chalk": "^1.1.1",
"connect": "^2.8.3", "connect": "^2.8.3",