From be4afdde37ab6ff6ebe573821745887fd3edfb05 Mon Sep 17 00:00:00 2001 From: Antoine Rousseau Date: Mon, 7 Nov 2016 10:37:11 -0800 Subject: [PATCH] Android WebSocket: include cookies in request Summary: This PR updates #6851 from srikanthkh, fixing coding conventions and javadoc, and adding a test plan. Added testing functions into the WebSocketExample page of the UIExplorer, including a tiny http server to set a cookie on demand. Instructions included in the UIExplorer app. Closes https://github.com/facebook/react-native/pull/9114 Differential Revision: D4140534 Pulled By: lacker fbshipit-source-id: e020ad0c6d1d3ea09c0c3564c1795b4e1bc4517d --- Examples/UIExplorer/js/WebSocketExample.js | 79 ++++++++++++++----- Examples/UIExplorer/js/http_test_server.js | 43 ++++++++++ .../UIExplorer/js/websocket_test_server.js | 5 +- .../com/facebook/react/modules/websocket/BUCK | 1 + .../modules/websocket/WebSocketModule.java | 46 +++++++++-- 5 files changed, 147 insertions(+), 27 deletions(-) create mode 100644 Examples/UIExplorer/js/http_test_server.js diff --git a/Examples/UIExplorer/js/WebSocketExample.js b/Examples/UIExplorer/js/WebSocketExample.js index 79e70a355..70585467c 100644 --- a/Examples/UIExplorer/js/WebSocketExample.js +++ b/Examples/UIExplorer/js/WebSocketExample.js @@ -32,10 +32,12 @@ const { Text, TextInput, TouchableOpacity, + ScrollView, View, } = ReactNative; const DEFAULT_WS_URL = 'ws://localhost:5555/'; +const DEFAULT_HTTP_URL = 'http://localhost:5556/'; const WS_EVENTS = [ 'close', 'error', @@ -95,6 +97,8 @@ function showValue(value) { type State = { url: string; + httpUrl: string; + fetchStatus: ?string; socket: ?WebSocket; socketState: ?number; lastSocketEvent: ?string; @@ -109,6 +113,8 @@ class WebSocketExample extends React.Component { state: State = { url: DEFAULT_WS_URL, + httpUrl: DEFAULT_HTTP_URL, + fetchStatus: null, socket: null, socketState: null, lastSocketEvent: null, @@ -154,6 +160,19 @@ class WebSocketExample extends React.Component { this.setState({outgoingMessage: ''}); }; + _sendHttp = () => { + this.setState({ + fetchStatus: 'fetching', + }); + fetch(this.state.httpUrl).then((response) => { + if (response.status >= 200 && response.status < 400) { + this.setState({ + fetchStatus: 'OK', + }); + } + }); + }; + _sendBinary = () => { if (!this.state.socket || typeof ArrayBuffer === 'undefined' || @@ -176,16 +195,11 @@ class WebSocketExample extends React.Component { this.state.socket.readyState >= WebSocket.CLOSING; const canSend = !!this.state.socket; return ( - + - Pro tip: + To start the WS test server: - node Examples/UIExplorer/websocket_test_server.js - - - {' in the '} - react-native - {' directory starts a test server.'} + ./Examples/UIExplorer/js/websocket_test_server.js { onChangeText={(url) => this.setState({url})} value={this.state.url} /> -