schat2-clive/src/app.js

80 lines
1.7 KiB
JavaScript

'use strict';
const config = require('config');
const bhttp = require('bhttp');
const WebSocket = require('ws');
async function auth() {
const httpSession = bhttp.session();
const res = await httpSession.post(`${config.api}/session`, config.user, {
encodeJSON: true,
});
if (res.statusCode !== 200) {
throw new Error(`Failed to authenticate: ${res.body.toString()}`);
}
return {
user: res.body,
httpSession,
sessionCookie: res.headers['set-cookie'][0],
};
}
async function getWsId(httpSession) {
const res = await httpSession.get(`${config.api}/socket`);
if (res.statusCode !== 200) {
throw new Error(`Failed to retrieve WebSocket ID: ${res.body.toString()}`);
}
return res.body;
}
function connect(wsCreds, sessionCookie) {
const ws = new WebSocket(`${config.socket}?${new URLSearchParams({ v: wsCreds.wsId, t: wsCreds.timestamp }).toString()}`, [], {
headers: {
cookie: sessionCookie,
},
});
return ws;
}
function onConnect(data, ws) {
ws.transmit('joinRooms', { rooms: config.channels });
}
function onRooms({ rooms }, ws) {
rooms.forEach((room) => {
ws.transmit('message', {
roomId: room.id,
body: `Hi, I am ${config.user.username}, your game host!`
});
});
}
const handlers = {
connect: onConnect,
rooms: onRooms,
};
async function init() {
const { user, httpSession, sessionCookie } = await auth();
const wsCreds = await getWsId(httpSession);
const ws = connect(wsCreds, sessionCookie);
ws.transmit = (domain, data) => {
ws.send(JSON.stringify([domain, data]));
}
ws.on('message', (msg) => {
const [domain, data] = JSON.parse(msg);
handlers[domain]?.(data, ws);
});
}
init();