Skip to content

Commit

Permalink
feat(engine): WS server moved to HTTP port
Browse files Browse the repository at this point in the history
Goodbye NODE_PORT (43595/43596)
  • Loading branch information
Pazaz committed Jan 29, 2025
1 parent 6cd757c commit 679ce76
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 21 deletions.
6 changes: 3 additions & 3 deletions public/client/Client.js

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';

import { startManagementWeb, startWeb } from '#/web.js';
import { startManagementWeb, startWeb, web } from '#/web.js';

import World from '#/engine/World.js';

Expand Down Expand Up @@ -42,18 +42,18 @@ if (Environment.EASY_STARTUP) {

await World.start();

const tcpServer = new TcpServer();
tcpServer.start();

const wsServer = new WSServer();
wsServer.start(web);

startWeb();
startManagementWeb();

register.setDefaultLabels({nodeId: Environment.NODE_ID});
collectDefaultMetrics({register});

const tcpServer = new TcpServer();
tcpServer.start();

const wsServer = new WSServer();
wsServer.start();

// unfortunately, tsx watch is not giving us a way to gracefully shut down in our dev mode:
// https://github.com/privatenumber/tsx/issues/494
let exiting = false;
Expand Down
4 changes: 2 additions & 2 deletions src/appMaintenance.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import TcpMaintenanceServer from '#/server/tcp/TcpMaintenanceServer.js';
import WSMaintenanceServer from '#/server/ws/WSMaintenanceServer.js';
import { startWeb } from '#/web.js';
import { startWeb, web } from '#/web.js';

startWeb();

const tcpServer = new TcpMaintenanceServer();
tcpServer.start();

const wsServer = new WSMaintenanceServer();
wsServer.start();
wsServer.start(web);
9 changes: 5 additions & 4 deletions src/server/ws/WSMaintenanceServer.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import http from 'http';
import { WebSocketServer, WebSocket } from 'ws';

import Packet from '#/io/Packet.js';

import Environment from '#/util/Environment.js';

// todo: websocket keepalives
export default class WSMaintenanceServer {
wss: WebSocketServer | null = null;

start() {
this.wss = new WebSocketServer({ port: Environment.NODE_PORT + 1, host: '0.0.0.0' }, () => {
start(server: http.Server) {
this.wss = new WebSocketServer({
server,
perMessageDeflate: false
});

this.wss.on('connection', (ws: WebSocket) => {
Expand Down
9 changes: 5 additions & 4 deletions src/server/ws/WSServer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import http, { IncomingMessage } from 'http';
import { WebSocketServer, WebSocket } from 'ws';
import { IncomingMessage } from 'http';

import Packet from '#/io/Packet.js';

import Environment from '#/util/Environment.js';
import NullClientSocket from '#/server/NullClientSocket.js';
import WSClientSocket from '#/server/ws/WSClientSocket.js';
import World from '#/engine/World.js';
Expand All @@ -27,8 +26,10 @@ function getIp(req: IncomingMessage) {
export default class WSServer {
wss: WebSocketServer | null = null;

start() {
this.wss = new WebSocketServer({ port: Environment.NODE_PORT + 1, host: '0.0.0.0' }, () => {
start(server: http.Server) {
this.wss = new WebSocketServer({
server,
perMessageDeflate: false
});

this.wss.on('connection', (ws: WebSocket, req) => {
Expand Down
2 changes: 1 addition & 1 deletion src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ MIME_TYPES.set('.wasm', 'application/wasm');
MIME_TYPES.set('.sf2', 'application/octet-stream');

// we don't need/want a full blown website or API on the game server
const web = http.createServer(async (req, res) => {
export const web = http.createServer(async (req, res) => {
try {
if (Environment.WEB_CORS) {
res.setHeader('Access-Control-Allow-Origin', '*');
Expand Down

0 comments on commit 679ce76

Please sign in to comment.