🎨 Add file/module name to logs and
order/refactor minimal code
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
import { IncomingMessage, Server } from "http";
|
||||
import { broadcast, handleClientAction } from "./chatClient";
|
||||
import { saveScheduledActions, scheduledActions } from "./scheduledActions";
|
||||
|
||||
import { AddressInfo } from "net";
|
||||
import { Socket } from "net";
|
||||
import WebSocket from "ws";
|
||||
import express from "express";
|
||||
import { handleClientAction } from "./chatClient";
|
||||
import { isDevelopment } from "./util";
|
||||
import { join } from "path";
|
||||
|
||||
const LOG_PREFIX_HTTP = "[HTTP] ";
|
||||
const LOG_PREFIX_WS = "[WS] ";
|
||||
|
||||
const app = express();
|
||||
const sockets: Array<WebSocket> = [];
|
||||
|
||||
@@ -20,8 +23,7 @@ let server: Server;
|
||||
|
||||
export {
|
||||
listen,
|
||||
// TODO: use intermediate class to handle socket messages
|
||||
sockets
|
||||
broadcast
|
||||
}
|
||||
|
||||
wsServer.on("connection", onConnection);
|
||||
@@ -30,7 +32,7 @@ app.use(express.static(join(process.cwd(), "client")));
|
||||
|
||||
function listen() {
|
||||
if (server) {
|
||||
console.log("[Webserver] Server is already running");
|
||||
console.log(`${LOG_PREFIX_HTTP}Server is already running`);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -42,7 +44,7 @@ function listen() {
|
||||
|
||||
function onListening() {
|
||||
console.log(
|
||||
`[Webserver] Listening on port ${(server.address() as AddressInfo).port}`
|
||||
`${LOG_PREFIX_HTTP}Listening on port ${(server.address() as AddressInfo).port}`
|
||||
);
|
||||
}
|
||||
|
||||
@@ -53,7 +55,7 @@ function onUpgrade(req: IncomingMessage, socket: Socket, head: Buffer) {
|
||||
}
|
||||
|
||||
function onConnection(socket: WebSocket, req: IncomingMessage) {
|
||||
console.log(`[WS] ${req.socket.remoteAddress} New connection established`);
|
||||
console.log(`${LOG_PREFIX_WS}${req.socket.remoteAddress} New connection established`);
|
||||
sockets.push(socket);
|
||||
socket.send(
|
||||
JSON.stringify({
|
||||
@@ -61,12 +63,22 @@ function onConnection(socket: WebSocket, req: IncomingMessage) {
|
||||
})
|
||||
);
|
||||
|
||||
socket.on("message", (msg: string) => onMessage(msg, socket));
|
||||
|
||||
socket.on("close", () => onClose(socket));
|
||||
socket.on("message", onMessage);
|
||||
socket.on("close", onClose);
|
||||
}
|
||||
|
||||
async function onMessage(msg: string, socket: WebSocket) {
|
||||
// broadcast a message to all clients
|
||||
function broadcast(msg: string, socket?: any) {
|
||||
const filteredSockets = socket
|
||||
? sockets.filter(s => s !== socket)
|
||||
: sockets;
|
||||
|
||||
filteredSockets.forEach(s => s.send(msg));
|
||||
}
|
||||
|
||||
async function onMessage(msg: string) {
|
||||
// @ts-ignore
|
||||
const socket = this as WebSocket;
|
||||
const data = JSON.parse(msg);
|
||||
|
||||
if (!data.actions || data.actions.length === 0) {
|
||||
@@ -84,11 +96,14 @@ async function onMessage(msg: string, socket: WebSocket) {
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`[WS] Received message with ${data.actions.length} actions:`, data);
|
||||
console.log(`${LOG_PREFIX_WS}Received message with ${data.actions.length} actions:`, data);
|
||||
}
|
||||
|
||||
function onClose(socket: WebSocket) {
|
||||
function onClose() {
|
||||
// @ts-ignore
|
||||
const socket: WebSocket = this as WebSocket;
|
||||
|
||||
const socketIdx = sockets.indexOf(socket);
|
||||
sockets.splice(socketIdx, 1);
|
||||
console.log("[WS] Connection closed");
|
||||
console.log(`${LOG_PREFIX_WS}Connection closed`);
|
||||
}
|
Reference in New Issue
Block a user