Forked from cihadturhan/attachWebsocketServer.diff
Created
November 21, 2018 10:47
-
-
Save Darkilen/149430b3095031ce197932e495d4c457 to your computer and use it in GitHub Desktop.
dirty hack to react-native/local-cli/server/util/attachWebsocketServer.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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. | |
* | |
* @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, | |
}); | |
+ let oldClient = null; | |
wss.on('connection', async ws => { | |
let connected = true; | |
const url = ws.upgradeReq.url; | |
const sendFn = (...args) => { | |
if (connected) { | |
ws.send(...args); | |
} | |
}; | |
+ if(oldClient){ | |
+ websocketServer.onClientDisconnect(oldClient); | |
+ } | |
const client = await websocketServer.onClientConnect(url, sendFn); | |
+ oldClient = client; | |
ws.on('error', e => { | |
websocketServer.onClientError && websocketServer.onClientError(client, e); | |
}); | |
ws.on('close', () => { | |
websocketServer.onClientDisconnect && | |
websocketServer.onClientDisconnect(client); | |
connected = false; | |
}); | |
ws.on('message', message => { | |
websocketServer.onClientMessage && | |
websocketServer.onClientMessage(client, message); | |
}); | |
}); | |
} | |
module.exports = attachWebsocketServer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment