react-native/RNTester/js/websocket_test_server.js
Eric Vicenti 4a80dceac7 Re-license and rename UIExplorer integration test app as RNTester
Reviewed By: yungsters

Differential Revision: D4950085

fbshipit-source-id: 44574b5d0ef0d2ad5dfc714309b18dc69cbad9ff
2017-05-08 11:31:19 -07:00

42 lines
1.1 KiB
JavaScript

#!/usr/bin/env node
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
* @providesModule websocket_test_server
*/
'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.
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:', message);
console.log('Cookie:', ws.upgradeReq.headers.cookie);
if (respondWithBinary) {
message = new Buffer(message);
}
ws.send(message);
});
console.log('Incoming connection!');
ws.send('Why hello there!');
});