2017-09-08 13:15:14 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
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-09-08 13:15:14 +00:00
|
|
|
*
|
|
|
|
* @format
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import type {Server as HTTPServer} from 'http';
|
|
|
|
import type {Server as HTTPSServer} from 'https';
|
|
|
|
|
|
|
|
type WebsocketServiceInterface<T> = {
|
|
|
|
+onClientConnect: (
|
|
|
|
url: string,
|
|
|
|
sendFn: (data: string) => mixed,
|
|
|
|
) => Promise<T>,
|
|
|
|
+onClientDisconnect?: (client: T) => mixed,
|
|
|
|
+onClientError?: (client: T, e: Error) => mixed,
|
|
|
|
+onClientMessage?: (client: T, message: string) => mixed,
|
|
|
|
};
|
|
|
|
|
|
|
|
type HMROptions<TClient> = {
|
|
|
|
httpServer: HTTPServer | HTTPSServer,
|
|
|
|
websocketServer: WebsocketServiceInterface<TClient>,
|
|
|
|
path: string,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attaches a WebSocket based connection to the Packager to expose
|
|
|
|
* Hot Module Replacement updates to the simulator.
|
|
|
|
*/
|
|
|
|
function attachWebsocketServer<TClient: Object>({
|
|
|
|
httpServer,
|
|
|
|
websocketServer,
|
|
|
|
path,
|
|
|
|
}: HMROptions<TClient>) {
|
|
|
|
const WebSocketServer = require('ws').Server;
|
|
|
|
const wss = new WebSocketServer({
|
|
|
|
server: httpServer,
|
|
|
|
path: path,
|
|
|
|
});
|
|
|
|
|
|
|
|
wss.on('connection', async ws => {
|
2017-09-30 00:56:26 +00:00
|
|
|
let connected = true;
|
2017-09-08 13:15:14 +00:00
|
|
|
const url = ws.upgradeReq.url;
|
|
|
|
|
2017-09-30 00:56:26 +00:00
|
|
|
const sendFn = (...args) => {
|
|
|
|
if (connected) {
|
|
|
|
ws.send(...args);
|
|
|
|
}
|
|
|
|
};
|
2017-09-08 13:15:14 +00:00
|
|
|
|
|
|
|
const client = await websocketServer.onClientConnect(url, sendFn);
|
|
|
|
|
|
|
|
ws.on('error', e => {
|
|
|
|
websocketServer.onClientError && websocketServer.onClientError(client, e);
|
|
|
|
});
|
|
|
|
|
|
|
|
ws.on('close', () => {
|
|
|
|
websocketServer.onClientDisconnect &&
|
|
|
|
websocketServer.onClientDisconnect(client);
|
2017-09-30 00:56:26 +00:00
|
|
|
connected = false;
|
2017-09-08 13:15:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
ws.on('message', message => {
|
|
|
|
websocketServer.onClientMessage &&
|
|
|
|
websocketServer.onClientMessage(client, message);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = attachWebsocketServer;
|