Pipe platform and bundleEntry through WebSocket connection

Reviewed By: vjeux

Differential Revision: D2793572

fb-gh-sync-id: 6ce2467b8d528d1a91c1b4fc51741f2502674022
This commit is contained in:
Martín Bigio
2015-12-29 18:24:06 -08:00
committed by facebook-github-bot-8
parent dd8e1f91d8
commit 5f850fbede
3 changed files with 41 additions and 18 deletions

View File

@@ -8,28 +8,30 @@
*/
'use strict';
const querystring = require('querystring');
const url = require('url');
/**
* Attaches a WebSocket based connection to the Packager to expose
* Hot Module Replacement updates to the simulator.
*/
function attachHMRServer({httpServer, path, packagerServer}) {
let activeWS;
let client = null;
function disconnect() {
activeWS = null;
client = null;
}
packagerServer.addFileChangeListener(filename => {
if (!activeWS) {
if (!client) {
return;
}
packagerServer.buildBundleForHMR({
entryFile: filename,
// TODO(martinb): receive platform on query string when client connects
platform: 'ios',
platform: client.platform,
})
.then(bundle => activeWS.send(bundle));
.then(bundle => client.ws.send(bundle));
});
const WebSocketServer = require('ws').Server;
@@ -41,14 +43,19 @@ function attachHMRServer({httpServer, path, packagerServer}) {
console.log('[Hot Module Replacement] Server listening on', path);
wss.on('connection', ws => {
console.log('[Hot Module Replacement] Client connected');
activeWS = ws;
const params = querystring.parse(url.parse(ws.upgradeReq.url).query);
client = {
ws,
platform: params.platform,
bundleEntry: params.bundleEntry,
};
ws.on('error', e => {
client.ws.on('error', e => {
console.error('[Hot Module Replacement] Unexpected error', e);
disconnect();
});
ws.on('close', () => disconnect());
client.ws.on('close', () => disconnect());
});
}