2015-03-19 19:10:41 +00:00
|
|
|
/**
|
2015-03-23 18:48:02 +00:00
|
|
|
* 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.
|
2015-03-19 19:10:41 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
|
|
function attachToServer(server, path) {
|
2015-10-12 23:46:52 +00:00
|
|
|
var WebSocketServer = require('ws').Server;
|
2015-03-19 19:10:41 +00:00
|
|
|
var wss = new WebSocketServer({
|
|
|
|
server: server,
|
|
|
|
path: path
|
|
|
|
});
|
2016-02-05 23:16:16 +00:00
|
|
|
var debuggerSocket, clientSocket;
|
2015-03-19 19:10:41 +00:00
|
|
|
|
2016-02-05 23:16:16 +00:00
|
|
|
function send(dest, message) {
|
|
|
|
if (!dest) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
dest.send(message);
|
2017-10-10 00:37:08 +00:00
|
|
|
} catch (e) {
|
2016-02-05 23:16:16 +00:00
|
|
|
console.warn(e);
|
|
|
|
// Sometimes this call throws 'not opened'
|
|
|
|
}
|
2015-07-23 23:56:23 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 19:10:41 +00:00
|
|
|
wss.on('connection', function(ws) {
|
2016-02-05 23:16:16 +00:00
|
|
|
const {url} = ws.upgradeReq;
|
2015-03-19 19:10:41 +00:00
|
|
|
|
2016-02-05 23:16:16 +00:00
|
|
|
if (url.indexOf('role=debugger') > -1) {
|
|
|
|
if (debuggerSocket) {
|
|
|
|
ws.close(1011, 'Another debugger is already connected');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
debuggerSocket = ws;
|
|
|
|
debuggerSocket.onerror =
|
|
|
|
debuggerSocket.onclose = () => {
|
|
|
|
debuggerSocket = null;
|
|
|
|
if (clientSocket) {
|
|
|
|
clientSocket.close(1011, 'Debugger was disconnected');
|
2015-04-24 21:57:24 +00:00
|
|
|
}
|
2016-02-05 23:16:16 +00:00
|
|
|
};
|
|
|
|
debuggerSocket.onmessage = ({data}) => send(clientSocket, data);
|
|
|
|
} else if (url.indexOf('role=client') > -1) {
|
|
|
|
if (clientSocket) {
|
|
|
|
clientSocket.onerror = clientSocket.onclose = clientSocket.onmessage = null;
|
|
|
|
clientSocket.close(1011, 'Another client connected');
|
|
|
|
}
|
|
|
|
clientSocket = ws;
|
|
|
|
clientSocket.onerror =
|
|
|
|
clientSocket.onclose = () => {
|
|
|
|
clientSocket = null;
|
|
|
|
send(debuggerSocket, JSON.stringify({method: '$disconnected'}));
|
|
|
|
};
|
|
|
|
clientSocket.onmessage = ({data}) => send(debuggerSocket, data);
|
|
|
|
} else {
|
|
|
|
ws.close(1011, 'Missing role param');
|
|
|
|
}
|
2015-03-19 19:10:41 +00:00
|
|
|
});
|
2015-10-05 16:15:10 +00:00
|
|
|
|
2015-10-23 18:28:49 +00:00
|
|
|
return {
|
|
|
|
server: wss,
|
2015-10-23 20:56:10 +00:00
|
|
|
isChromeConnected: function() {
|
2016-02-05 23:16:16 +00:00
|
|
|
return !!debuggerSocket;
|
2015-10-23 20:56:10 +00:00
|
|
|
}
|
2015-10-23 18:28:49 +00:00
|
|
|
};
|
2015-03-19 19:10:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
attachToServer: attachToServer
|
|
|
|
};
|