Use messageSocket to broadcast reload command on global shortcut
Reviewed By: yungsters Differential Revision: D3433464 fbshipit-source-id: 74111f419d224310b546e7c80fa871417436e1ab
This commit is contained in:
parent
b39fc05dbb
commit
bc8954babb
|
@ -15,70 +15,33 @@ function attachToServer(server, path) {
|
|||
server: server,
|
||||
path: path
|
||||
});
|
||||
var interfaceSocket, shellSocket;
|
||||
var clients = [];
|
||||
|
||||
function send(dest, message) {
|
||||
if (!dest) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
dest.send(message);
|
||||
} catch(e) {
|
||||
console.warn(e);
|
||||
// Sometimes this call throws 'not opened'
|
||||
}
|
||||
function sendFrom(source, data) {
|
||||
clients.forEach((client) => {
|
||||
if (client !== source) {
|
||||
try {
|
||||
client.send(data);
|
||||
} catch (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');
|
||||
}
|
||||
clients.push(ws);
|
||||
ws.onclose =
|
||||
ws.onerror = () => {
|
||||
ws.onmessage = null;
|
||||
clients = clients.filter((client) => client !== ws);
|
||||
};
|
||||
ws.onmessage = ({data}) => sendFrom(ws, data);
|
||||
});
|
||||
|
||||
return {
|
||||
server: wss,
|
||||
isChromeConnected: function() {
|
||||
return !!interfaceSocket;
|
||||
broadcast: (message) => {
|
||||
sendFrom(null, JSON.stringify(message));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue