This commit is contained in:
janderedev 2022-01-22 23:02:06 +01:00
parent 66232da6f8
commit ac632c9af7
No known key found for this signature in database
GPG key ID: 5D5E18ACB990F57A
2 changed files with 17 additions and 2 deletions

View file

@ -4,8 +4,10 @@
*/
import { WebSocketServer, WebSocket } from 'ws';
import { EventEmitter } from 'events';
import { logger } from "../..";
import server from '../../server';
if (!process.env.BOT_API_TOKEN) {
logger.error(`$BOT_API_TOKEN is not set. This token is `
+ `required for the bot to communicate with the API.`);
@ -14,6 +16,7 @@ if (!process.env.BOT_API_TOKEN) {
const { BOT_API_TOKEN } = process.env;
const wsServer = new WebSocketServer({ noServer: true });
const botWS = new EventEmitter();
const sockets: WebSocket[] = [];
wsServer.on('connection', (sock) => {
@ -27,7 +30,7 @@ wsServer.on('connection', (sock) => {
sock.on('message', (msg) => {
logger.debug(`[WS] [<] ${msg.toString()}`);
sock.send(JSON.stringify({ "h": JSON.parse(msg.toString()) }));
botWS.emit('message', JSON.parse(msg.toString()));
});
});
@ -51,3 +54,15 @@ server.on('upgrade', (req, socket, head) => {
socket.end();
}
});
function sendBotWS(msg: { [key: string]: any }) {
const socks = sockets.filter(sock => sock.readyState == sock.OPEN);
logger.debug(`[WS] [>] [${socks.length}] ${JSON.stringify(msg)}`);
socks.forEach(sock => sock.send(JSON.stringify(msg)));
}
botWS.on('message', msg => {
sendBotWS({ amogus: msg });
});
export { botWS, sendBotWS }

View file

@ -55,7 +55,7 @@ function connect() {
});
}
function wsSend(data: { [key: string]: string }) {
function wsSend(data: { [key: string]: any }) {
if (client && client.readyState == client.OPEN) {
logger.debug(`[WS] [>] ${JSON.stringify(data)}`);
client.send(JSON.stringify(data));