games_app/backend/dist/apps/ws.js

92 lines
3.8 KiB
JavaScript

"use strict";
/**
* WebSocket gateway — Candle Predict + Support messaging.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const http_1 = __importDefault(require("http"));
const ws_1 = require("ws");
const config_1 = require("../config");
const appBootstrap_1 = require("../bootstrap/appBootstrap");
const sentry_1 = require("../observability/sentry");
const auth_1 = require("../realtime/ws/auth");
const hub_1 = require("../realtime/ws/hub");
const supportHub_1 = require("../realtime/ws/supportHub");
const securityHub_1 = require("../realtime/ws/securityHub");
async function main() {
(0, sentry_1.initSentry)('ws');
if (!config_1.config.ws.enabled) {
console.error('[ws] WS_ENABLED is not true — exiting');
process.exit(1);
}
await (0, appBootstrap_1.bootstrapCore)({
startEngines: false,
startPlatformMonitors: false,
runDevMigrations: false,
});
await hub_1.gameWsHub.start();
await supportHub_1.supportWsHub.start();
await securityHub_1.securityWsHub.start();
const server = http_1.default.createServer((_req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Tloto WebSocket gateway');
});
const gameWss = new ws_1.WebSocketServer({ noServer: true });
const supportWss = new ws_1.WebSocketServer({ noServer: true });
const securityWss = new ws_1.WebSocketServer({ noServer: true });
gameWss.on('connection', (socket, req) => {
const url = new URL(req.url ?? '/', `http://${req.headers.host ?? 'localhost'}`);
const queryToken = url.searchParams.get('token') ?? undefined;
const headerToken = (0, auth_1.extractBearerToken)(req.headers.authorization);
const auth = (0, auth_1.authenticateWsToken)(headerToken ?? queryToken);
hub_1.gameWsHub.register(socket, auth);
socket.send(JSON.stringify({
type: 'connected',
authenticated: !!auth,
path: config_1.config.ws.path,
}));
});
supportWss.on('connection', (socket, req) => {
supportHub_1.supportWsHub.register(socket, req.url ?? '/', req.headers.authorization);
});
securityWss.on('connection', (socket, req) => {
securityHub_1.securityWsHub.register(socket, req.url ?? '/', req.headers.authorization);
});
server.on('upgrade', (req, socket, head) => {
const pathname = new URL(req.url ?? '/', `http://${req.headers.host ?? 'localhost'}`).pathname;
if (pathname === config_1.config.ws.path) {
gameWss.handleUpgrade(req, socket, head, (ws) => {
gameWss.emit('connection', ws, req);
});
return;
}
if (pathname === config_1.config.ws.supportPath) {
supportWss.handleUpgrade(req, socket, head, (ws) => {
supportWss.emit('connection', ws, req);
});
return;
}
if (pathname === config_1.config.ws.securityPath) {
securityWss.handleUpgrade(req, socket, head, (ws) => {
securityWss.emit('connection', ws, req);
});
return;
}
socket.destroy();
});
server.listen(config_1.config.ws.port, () => {
console.log(`[ws] listening on port ${config_1.config.ws.port}`);
console.log(`[ws] game path=${config_1.config.ws.path}`);
console.log(`[ws] support path=${config_1.config.ws.supportPath}`);
console.log(`[ws] security path=${config_1.config.ws.securityPath}`);
console.log(`[ws] redis=${config_1.config.redis.enabled}`);
});
}
main().catch((e) => {
(0, sentry_1.captureException)(e);
console.error(e);
process.exit(1);
});
//# sourceMappingURL=ws.js.map