2016-02-26 02:14:55 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-02-26 02:14:55 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2018-05-11 19:43:49 +00:00
|
|
|
*
|
|
|
|
* @format
|
2016-02-26 02:14:55 +00:00
|
|
|
*/
|
2018-05-11 19:43:49 +00:00
|
|
|
|
2016-02-26 02:14:55 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-02-16 12:20:32 +00:00
|
|
|
const url = require('url');
|
2017-02-14 17:37:35 +00:00
|
|
|
const WebSocketServer = require('ws').Server;
|
2017-02-16 12:20:32 +00:00
|
|
|
const PROTOCOL_VERSION = 2;
|
2017-09-08 22:15:51 +00:00
|
|
|
const notifier = require('node-notifier');
|
2017-02-14 17:37:35 +00:00
|
|
|
|
|
|
|
function parseMessage(data, binary) {
|
|
|
|
if (binary) {
|
|
|
|
console.error('Expected text message, got binary!');
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const message = JSON.parse(data);
|
|
|
|
if (message.version === PROTOCOL_VERSION) {
|
|
|
|
return message;
|
|
|
|
}
|
2018-05-11 19:43:49 +00:00
|
|
|
console.error(
|
|
|
|
'Received message had wrong protocol version: ' + message.version,
|
|
|
|
);
|
2017-02-14 17:37:35 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error('Failed to parse the message as JSON:\n' + data);
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
2016-02-26 02:14:55 +00:00
|
|
|
|
2017-02-16 12:20:32 +00:00
|
|
|
function isBroadcast(message) {
|
|
|
|
return (
|
|
|
|
typeof message.method === 'string' &&
|
|
|
|
message.id === undefined &&
|
|
|
|
message.target === undefined
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isRequest(message) {
|
|
|
|
return (
|
2018-05-11 19:43:49 +00:00
|
|
|
typeof message.method === 'string' && typeof message.target === 'string'
|
|
|
|
);
|
2017-02-16 12:20:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isResponse(message) {
|
|
|
|
return (
|
|
|
|
typeof message.id === 'object' &&
|
|
|
|
typeof message.id.requestId !== undefined &&
|
2018-05-11 19:43:49 +00:00
|
|
|
typeof message.id.clientId === 'string' &&
|
|
|
|
(message.result !== undefined || message.error !== undefined)
|
|
|
|
);
|
2017-02-16 12:20:32 +00:00
|
|
|
}
|
|
|
|
|
2016-02-26 02:14:55 +00:00
|
|
|
function attachToServer(server, path) {
|
2017-02-14 17:37:35 +00:00
|
|
|
const wss = new WebSocketServer({
|
2016-02-26 02:14:55 +00:00
|
|
|
server: server,
|
2018-05-11 19:43:49 +00:00
|
|
|
path: path,
|
2016-02-26 02:14:55 +00:00
|
|
|
});
|
2017-02-14 17:37:35 +00:00
|
|
|
const clients = new Map();
|
|
|
|
let nextClientId = 0;
|
2016-06-20 17:49:58 +00:00
|
|
|
|
2017-02-16 12:20:32 +00:00
|
|
|
function getClientWs(clientId) {
|
|
|
|
const clientWs = clients.get(clientId);
|
|
|
|
if (clientWs === undefined) {
|
|
|
|
throw `could not find id "${clientId}" while forwarding request`;
|
|
|
|
}
|
|
|
|
return clientWs;
|
|
|
|
}
|
|
|
|
|
2017-02-14 17:37:35 +00:00
|
|
|
function handleSendBroadcast(broadcasterId, message) {
|
|
|
|
const forwarded = {
|
|
|
|
version: PROTOCOL_VERSION,
|
2017-02-16 12:20:32 +00:00
|
|
|
method: message.method,
|
|
|
|
params: message.params,
|
2017-02-14 17:37:35 +00:00
|
|
|
};
|
2017-09-08 22:15:51 +00:00
|
|
|
if (clients.size === 0) {
|
|
|
|
notifier.notify({
|
2018-05-11 19:43:49 +00:00
|
|
|
title: 'React Native: No apps connected',
|
|
|
|
message:
|
|
|
|
`Sending '${message.method}' to all React Native apps ` +
|
|
|
|
'failed. Make sure your app is running in the simulator ' +
|
|
|
|
'or on a phone connected via USB.',
|
2017-09-08 22:15:51 +00:00
|
|
|
});
|
|
|
|
}
|
2017-02-14 17:37:35 +00:00
|
|
|
for (const [otherId, otherWs] of clients) {
|
|
|
|
if (otherId !== broadcasterId) {
|
2016-06-20 17:49:58 +00:00
|
|
|
try {
|
2017-02-14 17:37:35 +00:00
|
|
|
otherWs.send(JSON.stringify(forwarded));
|
2016-06-20 17:49:58 +00:00
|
|
|
} catch (e) {
|
2018-05-11 19:43:49 +00:00
|
|
|
console.error(
|
|
|
|
`Failed to send broadcast to client: '${otherId}' ` +
|
|
|
|
`due to:\n ${e.toString()}`,
|
|
|
|
);
|
2016-06-20 17:49:58 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-14 17:37:35 +00:00
|
|
|
}
|
2016-02-26 02:14:55 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 17:37:35 +00:00
|
|
|
wss.on('connection', function(clientWs) {
|
|
|
|
const clientId = `client#${nextClientId++}`;
|
|
|
|
|
2017-02-16 12:20:32 +00:00
|
|
|
function handleCaughtError(message, error) {
|
2017-02-14 17:37:35 +00:00
|
|
|
const errorMessage = {
|
2017-02-16 12:20:32 +00:00
|
|
|
id: message.id,
|
|
|
|
method: message.method,
|
2017-02-14 17:37:35 +00:00
|
|
|
target: message.target,
|
2017-02-16 12:20:32 +00:00
|
|
|
error: message.error === undefined ? 'undefined' : 'defined',
|
|
|
|
params: message.params === undefined ? 'undefined' : 'defined',
|
|
|
|
result: message.result === undefined ? 'undefined' : 'defined',
|
2017-02-14 17:37:35 +00:00
|
|
|
};
|
2017-02-16 12:20:32 +00:00
|
|
|
|
|
|
|
if (message.id === undefined) {
|
|
|
|
console.error(
|
|
|
|
`Handling message from ${clientId} failed with:\n${error}\n` +
|
2018-05-11 19:43:49 +00:00
|
|
|
`message:\n${JSON.stringify(errorMessage)}`,
|
|
|
|
);
|
2017-02-16 12:20:32 +00:00
|
|
|
} else {
|
|
|
|
try {
|
2018-05-11 19:43:49 +00:00
|
|
|
clientWs.send(
|
|
|
|
JSON.stringify({
|
|
|
|
version: PROTOCOL_VERSION,
|
|
|
|
error: error,
|
|
|
|
id: message.id,
|
|
|
|
}),
|
|
|
|
);
|
2017-02-16 12:20:32 +00:00
|
|
|
} catch (e) {
|
2018-05-11 19:43:49 +00:00
|
|
|
console.error(
|
|
|
|
`Failed to reply to ${clientId} with error:\n${error}` +
|
|
|
|
`\nmessage:\n${JSON.stringify(errorMessage)}` +
|
|
|
|
`\ndue to error: ${e.toString()}`,
|
|
|
|
);
|
2017-02-16 12:20:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleServerRequest(message) {
|
|
|
|
let result = null;
|
|
|
|
switch (message.method) {
|
|
|
|
case 'getid':
|
|
|
|
result = clientId;
|
|
|
|
break;
|
|
|
|
case 'getpeers':
|
|
|
|
result = {};
|
|
|
|
clients.forEach((otherWs, otherId) => {
|
|
|
|
if (clientId !== otherId) {
|
2017-03-17 16:55:23 +00:00
|
|
|
result[otherId] = url.parse(otherWs.upgradeReq.url, true).query;
|
2017-02-16 12:20:32 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
2018-01-13 06:03:51 +00:00
|
|
|
throw `unknown method: ${message.method}`;
|
2017-02-16 12:20:32 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
clientWs.send(
|
|
|
|
JSON.stringify({
|
|
|
|
version: PROTOCOL_VERSION,
|
|
|
|
result: result,
|
|
|
|
id: message.id,
|
|
|
|
}),
|
|
|
|
);
|
2017-02-16 12:20:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function forwardRequest(message) {
|
2018-05-11 19:43:49 +00:00
|
|
|
getClientWs(message.target).send(
|
|
|
|
JSON.stringify({
|
|
|
|
version: PROTOCOL_VERSION,
|
|
|
|
method: message.method,
|
|
|
|
params: message.params,
|
|
|
|
id:
|
|
|
|
message.id === undefined
|
|
|
|
? undefined
|
|
|
|
: {requestId: message.id, clientId: clientId},
|
|
|
|
}),
|
|
|
|
);
|
2017-02-16 12:20:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function forwardResponse(message) {
|
2018-05-11 19:43:49 +00:00
|
|
|
getClientWs(message.id.clientId).send(
|
|
|
|
JSON.stringify({
|
|
|
|
version: PROTOCOL_VERSION,
|
|
|
|
result: message.result,
|
|
|
|
error: message.error,
|
|
|
|
id: message.id.requestId,
|
|
|
|
}),
|
|
|
|
);
|
2017-02-14 17:37:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clients.set(clientId, clientWs);
|
2018-05-11 19:43:49 +00:00
|
|
|
clientWs.onclose = clientWs.onerror = () => {
|
2017-02-14 17:37:35 +00:00
|
|
|
clientWs.onmessage = null;
|
|
|
|
clients.delete(clientId);
|
|
|
|
};
|
2018-05-11 19:43:49 +00:00
|
|
|
clientWs.onmessage = event => {
|
2017-02-14 17:37:35 +00:00
|
|
|
const message = parseMessage(event.data, event.binary);
|
|
|
|
if (message === undefined) {
|
|
|
|
console.error('Received message not matching protocol');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-02-16 12:20:32 +00:00
|
|
|
if (isBroadcast(message)) {
|
|
|
|
handleSendBroadcast(clientId, message);
|
|
|
|
} else if (isRequest(message)) {
|
|
|
|
if (message.target === 'server') {
|
|
|
|
handleServerRequest(message);
|
|
|
|
} else {
|
|
|
|
forwardRequest(message);
|
|
|
|
}
|
|
|
|
} else if (isResponse(message)) {
|
|
|
|
forwardResponse(message);
|
|
|
|
} else {
|
|
|
|
throw 'Invalid message, did not match the protocol';
|
|
|
|
}
|
2017-02-14 17:37:35 +00:00
|
|
|
} catch (e) {
|
2017-02-16 12:20:32 +00:00
|
|
|
handleCaughtError(message, e.toString());
|
2017-02-14 17:37:35 +00:00
|
|
|
}
|
2016-06-20 17:49:58 +00:00
|
|
|
};
|
2016-02-26 02:14:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
2017-02-16 12:20:32 +00:00
|
|
|
broadcast: (method, params) => {
|
|
|
|
handleSendBroadcast(null, {method: method, params: params});
|
2018-05-11 19:43:49 +00:00
|
|
|
},
|
2016-02-26 02:14:55 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2017-02-14 17:37:35 +00:00
|
|
|
attachToServer: attachToServer,
|
|
|
|
parseMessage: parseMessage,
|
2016-02-26 02:14:55 +00:00
|
|
|
};
|