Skip to content

Commit

Permalink
Add message channel to packager for sending commands to bridge (2/N)
Browse files Browse the repository at this point in the history
Reviewed By: frantic

Differential Revision: D2957599

fb-gh-sync-id: 0d3f39bb5757c7af8ee6d178f4839af81928b8de
shipit-source-id: 0d3f39bb5757c7af8ee6d178f4839af81928b8de
  • Loading branch information
bottledwalter authored and Facebook Github Bot 7 committed Feb 26, 2016
1 parent 7eb7b94 commit c377f2a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
4 changes: 4 additions & 0 deletions local-cli/server/runServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const getDevToolsMiddleware = require('./middleware/getDevToolsMiddleware');
const http = require('http');
const isAbsolutePath = require('absolute-path');
const loadRawBodyMiddleware = require('./middleware/loadRawBodyMiddleware');
const messageSocket = require('./util/messageSocket.js');
const openStackFrameInEditorMiddleware = require('./middleware/openStackFrameInEditorMiddleware');
const path = require('path');
const ReactPackager = require('../../packager/react-packager');
Expand All @@ -24,11 +25,13 @@ const webSocketProxy = require('./util/webSocketProxy.js');

function runServer(args, config, readyCallback) {
var wsProxy = null;
var ms = null;
const packagerServer = getPackagerServer(args, config);
const app = connect()
.use(loadRawBodyMiddleware)
.use(connect.compress())
.use(getDevToolsMiddleware(args, () => wsProxy && wsProxy.isChromeConnected()))
.use(getDevToolsMiddleware(args, () => ms && ms.isChromeConnected()))
.use(openStackFrameInEditorMiddleware)
.use(statusPageMiddleware)
.use(systraceProfileMiddleware)
Expand All @@ -51,6 +54,7 @@ function runServer(args, config, readyCallback) {
});

wsProxy = webSocketProxy.attachToServer(serverInstance, '/debugger-proxy');
ms = messageSocket.attachToServer(serverInstance, '/message');
webSocketProxy.attachToServer(serverInstance, '/devtools');
readyCallback();
}
Expand Down
88 changes: 88 additions & 0 deletions local-cli/server/util/messageSocket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* 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.
*/
'use strict';


function attachToServer(server, path) {
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({
server: server,
path: path
});
var interfaceSocket, shellSocket;

function send(dest, message) {
if (!dest) {
return;
}

try {
dest.send(message);
} catch(e) {
console.warn(e);
// Sometimes this call throws 'not opened'
}
}

wss.on('connection', function(ws) {
const {url} = ws.upgradeReq;

if (url.indexOf('role=interface') > -1) {
if (interfaceSocket) {
ws.close(1011, 'Another debugger is already connected');
return;
}
interfaceSocket = ws;
interfaceSocket.onerror =
interfaceSocket.onclose = () => {
interfaceSocket = null;
// if (shellSocket) {
// shellSocket.close(1011, 'Interface was disconnected');
// }
};

interfaceSocket.onmessage = ({data}) => {
send(shellSocket, data)
};
} else if (url.indexOf('role=shell') > -1) {
if (shellSocket) {
shellSocket.onerror = shellSocket.onclose = shellSocket.onmessage = null;
shellSocket.close(1011, 'Another client connected');
}
shellSocket = ws;
shellSocket.onerror =
shellSocket.onclose = () => {
shellSocket = null;
send(interfaceSocket, JSON.stringify({method: '$disconnected'}));
};
shellSocket.onmessage = ({data}) => send(interfaceSocket, data);

// console.log('CLIENT ----');
// if (doIt) {
// console.log('> sending: %s', str);
// send(shellSocket, str);
// console.log('< sending');
// }

} else {
ws.close(1011, 'Missing role param');
}
});

return {
server: wss,
isChromeConnected: function() {
return !!interfaceSocket;
}
};
}

module.exports = {
attachToServer: attachToServer
};

1 comment on commit c377f2a

@atticoos
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bottledwalter would you mind summarizing what this does? This looks very interesting, is there an issue you could refer to?

It looks like it allows the packager to let the application know about certain events?

Please sign in to comment.