2016-02-26 02:14:55 +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.
|
|
|
|
*/
|
|
|
|
'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-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;
|
|
|
|
}
|
|
|
|
console.error('Received message had wrong protocol version: '
|
|
|
|
+ message.version);
|
|
|
|
} 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 (
|
|
|
|
typeof message.method === 'string' &&
|
|
|
|
typeof message.target === 'string');
|
|
|
|
}
|
|
|
|
|
|
|
|
function isResponse(message) {
|
|
|
|
return (
|
|
|
|
typeof message.id === 'object' &&
|
|
|
|
typeof message.id.requestId !== undefined &&
|
|
|
|
typeof message.id.clientId === 'string' && (
|
|
|
|
message.result !== undefined ||
|
|
|
|
message.error !== undefined
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
path: path
|
|
|
|
});
|
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
|
|
|
};
|
|
|
|
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) {
|
2017-02-14 17:37:35 +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` +
|
|
|
|
`message:\n${JSON.stringify(errorMessage)}`);
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
clientWs.send(JSON.stringify({
|
|
|
|
version: PROTOCOL_VERSION,
|
|
|
|
error: error,
|
|
|
|
id: message.id,
|
|
|
|
}));
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`Failed to reply to ${clientId} with error:\n${error}` +
|
|
|
|
`\nmessage:\n${JSON.stringify(errorMessage)}` +
|
|
|
|
`\ndue to error: ${e.toString()}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
throw `unkown method: ${message.method}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
clientWs.send(JSON.stringify({
|
|
|
|
version: PROTOCOL_VERSION,
|
|
|
|
result: result,
|
|
|
|
id: message.id
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function forwardRequest(message) {
|
|
|
|
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}),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function forwardResponse(message) {
|
|
|
|
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);
|
|
|
|
clientWs.onclose =
|
|
|
|
clientWs.onerror = () => {
|
|
|
|
clientWs.onmessage = null;
|
|
|
|
clients.delete(clientId);
|
|
|
|
};
|
|
|
|
clientWs.onmessage = (event) => {
|
|
|
|
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});
|
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
|
|
|
};
|