mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-23 03:50:11 +08:00
Clean up and simplify WebSocket implementation on the JS side
Summary:- Get rid of no longer necessary WebSocket.js v WebSocketBase.js split - Use `EventTarget(list, of, events)` as base class to auto-generate `oneventname` getters/setters that get invoked along with other event handlers - Type annotation `any` considered harmful, especially when we can easily spell out the actual type - Throw in some `const` goodness for free **Test Plan:** Launch UIExplorer example app, supplied `websocket_test_server` script, and try different combinations of sending and receiving text and binary data on both iOS and Android. Closes https://github.com/facebook/react-native/pull/6889 Differential Revision: D3184835 Pulled By: mkonicek fb-gh-sync-id: f21707f4e97aa5a79847f5157e0a9f132a1a01cd fbshipit-source-id: f21707f4e97aa5a79847f5157e0a9f132a1a01cd
This commit is contained in:
committed by
Facebook Github Bot 0
parent
0fa48c00a9
commit
ebb44d202b
43
Examples/UIExplorer/websocket_test_server.js
Normal file
43
Examples/UIExplorer/websocket_test_server.js
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* The examples provided by Facebook are for non-commercial testing and
|
||||
* evaluation purposes only.
|
||||
*
|
||||
* Facebook reserves all rights not expressly granted.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
|
||||
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* @flow
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
/* eslint-env node */
|
||||
|
||||
const WebSocket = require('ws');
|
||||
|
||||
console.log(`\
|
||||
Test server for WebSocketExample
|
||||
|
||||
This will send each incoming message right back to the other side.a
|
||||
Restart with the '--binary' command line flag to have it respond with an
|
||||
ArrayBuffer instead of a string.
|
||||
`);
|
||||
|
||||
const respondWithBinary = process.argv.indexOf('--binary') !== -1;
|
||||
const server = new WebSocket.Server({port: 5555});
|
||||
server.on('connection', (ws) => {
|
||||
ws.on('message', (message) => {
|
||||
console.log('Received message: %s', message);
|
||||
if (respondWithBinary) {
|
||||
message = new Buffer(message);
|
||||
}
|
||||
ws.send(message);
|
||||
});
|
||||
|
||||
console.log('Incoming connection!');
|
||||
ws.send('Why hello there!');
|
||||
});
|
||||
Reference in New Issue
Block a user