2017-01-16 21:23:33 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2017-01-16 21:23:33 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-01-16 21:23:33 +00:00
|
|
|
*
|
2018-05-10 22:44:55 +00:00
|
|
|
* @format
|
2017-01-16 21:23:33 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-10 22:44:55 +00:00
|
|
|
|
2017-01-16 21:23:33 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-05-14 07:09:36 +00:00
|
|
|
const React = require('react');
|
|
|
|
const ReactNative = require('react-native');
|
|
|
|
const {View} = ReactNative;
|
|
|
|
const {TestModule} = ReactNative.NativeModules;
|
2017-01-16 21:23:33 +00:00
|
|
|
|
|
|
|
const DEFAULT_WS_URL = 'ws://localhost:5555/';
|
|
|
|
|
2018-05-10 22:44:55 +00:00
|
|
|
const WS_EVENTS = ['close', 'error', 'message', 'open'];
|
2017-01-16 21:23:33 +00:00
|
|
|
const WS_STATES = [
|
|
|
|
/* 0 */ 'CONNECTING',
|
|
|
|
/* 1 */ 'OPEN',
|
|
|
|
/* 2 */ 'CLOSING',
|
|
|
|
/* 3 */ 'CLOSED',
|
|
|
|
];
|
|
|
|
|
|
|
|
type State = {
|
2018-05-10 22:44:55 +00:00
|
|
|
url: string,
|
|
|
|
fetchStatus: ?string,
|
|
|
|
socket: ?WebSocket,
|
|
|
|
socketState: ?number,
|
|
|
|
lastSocketEvent: ?string,
|
|
|
|
lastMessage: ?string | ?ArrayBuffer,
|
|
|
|
testMessage: string,
|
|
|
|
testExpectedResponse: string,
|
2017-01-16 21:23:33 +00:00
|
|
|
};
|
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class WebSocketTest extends React.Component<{}, State> {
|
2017-01-16 21:23:33 +00:00
|
|
|
state: State = {
|
|
|
|
url: DEFAULT_WS_URL,
|
|
|
|
fetchStatus: null,
|
|
|
|
socket: null,
|
|
|
|
socketState: null,
|
|
|
|
lastSocketEvent: null,
|
|
|
|
lastMessage: null,
|
|
|
|
testMessage: 'testMessage',
|
2018-05-10 22:44:55 +00:00
|
|
|
testExpectedResponse: 'testMessage_response',
|
2017-01-16 21:23:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
_waitFor = (condition: any, timeout: any, callback: any) => {
|
2018-05-14 07:09:36 +00:00
|
|
|
let remaining = timeout;
|
|
|
|
let t;
|
|
|
|
const timeoutFunction = function() {
|
2017-01-16 21:23:33 +00:00
|
|
|
if (condition()) {
|
|
|
|
callback(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
remaining--;
|
|
|
|
if (remaining === 0) {
|
|
|
|
callback(false);
|
|
|
|
} else {
|
2018-05-10 22:44:55 +00:00
|
|
|
t = setTimeout(timeoutFunction, 1000);
|
2017-01-16 21:23:33 +00:00
|
|
|
}
|
|
|
|
};
|
2018-05-10 22:44:55 +00:00
|
|
|
t = setTimeout(timeoutFunction, 1000);
|
|
|
|
};
|
2017-01-16 21:23:33 +00:00
|
|
|
|
|
|
|
_connect = () => {
|
|
|
|
const socket = new WebSocket(this.state.url);
|
|
|
|
WS_EVENTS.forEach(ev => socket.addEventListener(ev, this._onSocketEvent));
|
|
|
|
this.setState({
|
|
|
|
socket,
|
|
|
|
socketState: socket.readyState,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
_socketIsConnected = () => {
|
|
|
|
return this.state.socketState === 1; //'OPEN'
|
2018-05-10 22:44:55 +00:00
|
|
|
};
|
2017-01-16 21:23:33 +00:00
|
|
|
|
|
|
|
_socketIsDisconnected = () => {
|
|
|
|
return this.state.socketState === 3; //'CLOSED'
|
2018-05-10 22:44:55 +00:00
|
|
|
};
|
2017-01-16 21:23:33 +00:00
|
|
|
|
|
|
|
_disconnect = () => {
|
|
|
|
if (!this.state.socket) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.state.socket.close();
|
|
|
|
};
|
|
|
|
|
|
|
|
_onSocketEvent = (event: any) => {
|
|
|
|
const state: any = {
|
|
|
|
socketState: event.target.readyState,
|
|
|
|
lastSocketEvent: event.type,
|
|
|
|
};
|
|
|
|
if (event.type === 'message') {
|
|
|
|
state.lastMessage = event.data;
|
|
|
|
}
|
|
|
|
this.setState(state);
|
|
|
|
};
|
|
|
|
|
|
|
|
_sendText = (text: string) => {
|
|
|
|
if (!this.state.socket) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.state.socket.send(text);
|
|
|
|
};
|
|
|
|
|
|
|
|
_sendTestMessage = () => {
|
|
|
|
this._sendText(this.state.testMessage);
|
|
|
|
};
|
|
|
|
|
|
|
|
_receivedTestExpectedResponse = () => {
|
2018-05-10 22:44:55 +00:00
|
|
|
return this.state.lastMessage === this.state.testExpectedResponse;
|
2017-01-16 21:23:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.testConnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
testConnect = () => {
|
2018-05-14 07:09:36 +00:00
|
|
|
const component = this;
|
2017-01-16 21:23:33 +00:00
|
|
|
component._connect();
|
2018-05-10 22:44:55 +00:00
|
|
|
component._waitFor(component._socketIsConnected, 5, function(
|
|
|
|
connectSucceeded,
|
|
|
|
) {
|
2017-01-16 21:23:33 +00:00
|
|
|
if (!connectSucceeded) {
|
|
|
|
TestModule.markTestPassed(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
component.testSendAndReceive();
|
|
|
|
});
|
2018-05-10 22:44:55 +00:00
|
|
|
};
|
2017-01-16 21:23:33 +00:00
|
|
|
|
|
|
|
testSendAndReceive = () => {
|
2018-05-14 07:09:36 +00:00
|
|
|
const component = this;
|
2017-01-16 21:23:33 +00:00
|
|
|
component._sendTestMessage();
|
2018-05-10 22:44:55 +00:00
|
|
|
component._waitFor(component._receivedTestExpectedResponse, 5, function(
|
|
|
|
messageReceived,
|
|
|
|
) {
|
2017-01-16 21:23:33 +00:00
|
|
|
if (!messageReceived) {
|
|
|
|
TestModule.markTestPassed(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
component.testDisconnect();
|
|
|
|
});
|
2018-05-10 22:44:55 +00:00
|
|
|
};
|
2017-01-16 21:23:33 +00:00
|
|
|
|
|
|
|
testDisconnect = () => {
|
2018-05-14 07:09:36 +00:00
|
|
|
const component = this;
|
2017-01-16 21:23:33 +00:00
|
|
|
component._disconnect();
|
2018-05-10 22:44:55 +00:00
|
|
|
component._waitFor(component._socketIsDisconnected, 5, function(
|
|
|
|
disconnectSucceeded,
|
|
|
|
) {
|
2017-01-16 21:23:33 +00:00
|
|
|
TestModule.markTestPassed(disconnectSucceeded);
|
|
|
|
});
|
2018-05-10 22:44:55 +00:00
|
|
|
};
|
2017-01-16 21:23:33 +00:00
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
render(): React.Node {
|
2017-01-16 21:23:33 +00:00
|
|
|
return <View />;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketTest.displayName = 'WebSocketTest';
|
|
|
|
|
|
|
|
module.exports = WebSocketTest;
|