From afbff9bf880704bea93b3c071ba0d5a9f1f303f6 Mon Sep 17 00:00:00 2001 From: Johan Lindskogen Date: Tue, 22 Dec 2015 09:22:01 -0800 Subject: [PATCH] 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 --- Libraries/WebSocket/RCTWebSocketModule.m | 4 +- Libraries/WebSocket/WebSocket.js | 4 +- npm-shrinkwrap.json | 113 ++++++++++++----------- package.json | 1 + 4 files changed, 66 insertions(+), 56 deletions(-) diff --git a/Libraries/WebSocket/RCTWebSocketModule.m b/Libraries/WebSocket/RCTWebSocketModule.m index a19335331..7afa7dace 100644 --- a/Libraries/WebSocket/RCTWebSocketModule.m +++ b/Libraries/WebSocket/RCTWebSocketModule.m @@ -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 }]; } diff --git a/Libraries/WebSocket/WebSocket.js b/Libraries/WebSocket/WebSocket.js index 3ce40bc67..515a6ad5c 100644 --- a/Libraries/WebSocket/WebSocket.js +++ b/Libraries/WebSocket/WebSocket.js @@ -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); diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 3820eda08..96c371e0f 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -151,16 +151,16 @@ "from": "globals@>=6.4.0 <7.0.0", "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": { "version": "1.0.1", "from": "js-tokens@1.0.1", "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": { "version": "0.8.40", "from": "regenerator@0.8.40", @@ -284,16 +284,16 @@ "from": "globals@>=6.4.0 <7.0.0", "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": { "version": "0.10.33", "from": "recast@0.10.33", "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": { "version": "0.8.40", "from": "regenerator@0.8.40", @@ -663,6 +663,11 @@ "from": "balanced-match@>=0.2.0 <0.3.0", "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": { "version": "1.1.0", "from": "beeper@>=1.0.0 <2.0.0", @@ -1855,16 +1860,16 @@ "from": "asn1@0.1.11", "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": { "version": "0.1.5", "from": "assert-plus@^0.1.5", "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": { "version": "0.6.0", "from": "aws-sign2@~0.6.0", @@ -1880,16 +1885,16 @@ "from": "block-stream@*", "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": { "version": "2.10.1", "from": "boom@^2.8.x", "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": { "version": "0.11.0", "from": "caseless@~0.11.0", @@ -1945,16 +1950,16 @@ "from": "delayed-stream@~1.0.0", "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": { "version": "0.1.0", "from": "delegates@^0.1.0", "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": { "version": "3.0.0", "from": "extend@~3.0.0", @@ -2010,16 +2015,16 @@ "from": "has-ansi@^2.0.0", "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": { "version": "1.0.1", "from": "has-unicode@^1.0.0", "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": { "version": "2.16.3", "from": "hoek@2.x.x", @@ -2030,16 +2035,16 @@ "from": "http-signature@~0.11.0", "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": { "version": "2.0.1", "from": "inherits@*", "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": { "version": "2.12.2", "from": "is-my-json-valid@^2.12.2", @@ -2150,41 +2155,41 @@ "from": "pinkie@^1.0.0", "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": { "version": "1.0.0", "from": "pinkie-promise@^1.0.0", "resolved": "https://registry.npmjs.org/pinkie-promise/-/pinkie-promise-1.0.0.tgz" }, - "request": { - "version": "2.65.0", - "from": "request@2.x", - "resolved": "https://registry.npmjs.org/request/-/request-2.65.0.tgz" + "qs": { + "version": "5.2.0", + "from": "qs@~5.2.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-5.2.0.tgz" }, "readable-stream": { "version": "1.1.13", "from": "readable-stream@^1.1.13", "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": { "version": "5.0.3", "from": "semver@~5.0.1", "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": { "version": "1.0.9", "from": "sntp@1.x.x", "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": { "version": "0.0.5", "from": "stringstream@~0.0.4", @@ -2210,16 +2215,16 @@ "from": "tar@~2.2.0", "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": { "version": "2.2.0", "from": "tough-cookie@~2.2.0", "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": { "version": "0.0.3", "from": "uid-number@0.0.3", diff --git a/package.json b/package.json index 739f72859..7f47f5a9b 100644 --- a/package.json +++ b/package.json @@ -84,6 +84,7 @@ "babel-polyfill": "^6.0.16", "babel-types": "^6.1.2", "babylon": "^6.1.2", + "base64-js": "^0.0.8", "bser": "^1.0.2", "chalk": "^1.1.1", "connect": "^2.8.3",