Add support for receiving binary type data (ArrayBuffer)
Summary:This brings the same functionality that's already present on iOS, introduced in #4483, to Android: convert binary payloads to base64 strings and send them to JS land that way, where they'll be turned into an ArrayBuffer. **Test Plan:** Used test server from #6889 (in `--binary` mode) to send some binary data to the Android UIExplorer example (also from #6889). Verified it's received correctly as `ArrayBuffer`. Closes https://github.com/facebook/react-native/pull/6868 Differential Revision: D3184797 Pulled By: mkonicek fb-gh-sync-id: e78c640c43b3e41a75ddba79acc04e5eaab5667d fbshipit-source-id: e78c640c43b3e41a75ddba79acc04e5eaab5667d
This commit is contained in:
parent
a3ef44b4ba
commit
20cae51a8f
|
@ -9,6 +9,8 @@
|
||||||
|
|
||||||
package com.facebook.react.modules.websocket;
|
package com.facebook.react.modules.websocket;
|
||||||
|
|
||||||
|
import android.util.Base64;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.IllegalStateException;
|
import java.lang.IllegalStateException;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
@ -145,7 +147,11 @@ public class WebSocketModule extends ReactContextBaseJavaModule {
|
||||||
public void onMessage(BufferedSource bufferedSource, WebSocket.PayloadType payloadType) {
|
public void onMessage(BufferedSource bufferedSource, WebSocket.PayloadType payloadType) {
|
||||||
String message;
|
String message;
|
||||||
try {
|
try {
|
||||||
message = bufferedSource.readUtf8();
|
if (payloadType == WebSocket.PayloadType.BINARY) {
|
||||||
|
message = Base64.encodeToString(bufferedSource.readByteArray(), Base64.NO_WRAP);
|
||||||
|
} else {
|
||||||
|
message = bufferedSource.readUtf8();
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
notifyWebSocketFailed(id, e.getMessage());
|
notifyWebSocketFailed(id, e.getMessage());
|
||||||
return;
|
return;
|
||||||
|
@ -162,6 +168,7 @@ public class WebSocketModule extends ReactContextBaseJavaModule {
|
||||||
WritableMap params = Arguments.createMap();
|
WritableMap params = Arguments.createMap();
|
||||||
params.putInt("id", id);
|
params.putInt("id", id);
|
||||||
params.putString("data", message);
|
params.putString("data", message);
|
||||||
|
params.putString("type", payloadType == WebSocket.PayloadType.BINARY ? "binary" : "text");
|
||||||
sendEvent("websocketMessage", params);
|
sendEvent("websocketMessage", params);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue