/** * Copyright (c) 2015-present, Facebook, Inc. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @providesModule WebSocketExample * @format */ 'use strict'; /* eslint-env browser */ const React = require('react'); const ReactNative = require('react-native'); const { Image, PixelRatio, ScrollView, StyleSheet, Text, TextInput, TouchableOpacity, View, } = ReactNative; const DEFAULT_WS_URL = 'ws://localhost:5555/'; const DEFAULT_HTTP_URL = 'http://localhost:5556/'; const WS_EVENTS = ['close', 'error', 'message', 'open']; const WS_STATES = [ /* 0 */ 'CONNECTING', /* 1 */ 'OPEN', /* 2 */ 'CLOSING', /* 3 */ 'CLOSED', ]; class Button extends React.Component { render(): React.Element { const label = {this.props.label}; if (this.props.disabled) { return ( {label} ); } return ( {label} ); } } class Row extends React.Component { render(): React.Element { return ( {this.props.label} {this.props.value ? {this.props.value} : null} {this.props.children} ); } } class WebSocketImage extends React.Component { ws: ?WebSocket = null; state: {blob: ?Blob} = {blob: null}; componentDidMount() { let ws = (this.ws = new WebSocket(this.props.url)); ws.binaryType = 'blob'; ws.onmessage = event => { if (event.data instanceof Blob) { const blob = event.data; if (this.state.blob) { this.state.blob.close(); } this.setState({blob}); } }; ws.onopen = event => { ws.send('getImage'); }; } componentUnmount() { if (this.state.blob) { this.state.blob.close(); } this.ws && this.ws.close(); } render() { if (!this.state.blob) { return ; } return ( ); } } function showValue(value) { if (value === undefined || value === null) { return '(no value)'; } if ( typeof ArrayBuffer !== 'undefined' && typeof Uint8Array !== 'undefined' && value instanceof ArrayBuffer ) { return `ArrayBuffer {${String(Array.from(new Uint8Array(value)))}}`; } return value; } type State = { url: string, httpUrl: string, fetchStatus: ?string, socket: ?WebSocket, socketState: ?number, lastSocketEvent: ?string, lastMessage: ?string | ?ArrayBuffer, outgoingMessage: string, }; class WebSocketExample extends React.Component { static title = 'WebSocket'; static description = 'WebSocket API'; state: State = { url: DEFAULT_WS_URL, httpUrl: DEFAULT_HTTP_URL, fetchStatus: null, socket: null, socketState: null, lastSocketEvent: null, lastMessage: null, outgoingMessage: '', }; _connect = () => { const socket = new WebSocket(this.state.url); WS_EVENTS.forEach(ev => socket.addEventListener(ev, this._onSocketEvent)); this.setState({ socket, socketState: socket.readyState, }); }; _disconnect = () => { if (!this.state.socket) { return; } this.state.socket.close(); }; _onSocketEvent = (event: MessageEvent) => { const state: any = { socketState: event.target.readyState, lastSocketEvent: event.type, }; if (event.type === 'message') { state.lastMessage = event.data; } this.setState(state); }; _sendText = () => { if (!this.state.socket) { return; } this.state.socket.send(this.state.outgoingMessage); 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' || typeof Uint8Array === 'undefined' ) { return; } const {outgoingMessage} = this.state; const buffer = new Uint8Array(outgoingMessage.length); for (let i = 0; i < outgoingMessage.length; i++) { buffer[i] = outgoingMessage.charCodeAt(i); } this.state.socket.send(buffer); this.setState({outgoingMessage: ''}); }; render(): React.Element { const socketState = WS_STATES[this.state.socketState || -1]; const canConnect = !this.state.socket || this.state.socket.readyState >= WebSocket.CLOSING; const canSend = socketState === 'OPEN'; return ( To start the WS test server: ./RNTester/js/websocket_test_server.js {canSend ? : null} this.setState({url})} value={this.state.url} />