2022-01-08 17:17:43 +01:00
|
|
|
import * as fs from "fs";
|
|
|
|
import * as https from "https";
|
2022-01-07 22:21:42 +01:00
|
|
|
import { WebSocketServer } from "ws";
|
2022-01-08 17:17:43 +01:00
|
|
|
|
|
|
|
import * as crypto from "crypto";
|
|
|
|
|
2022-01-08 20:23:03 +01:00
|
|
|
import type {
|
|
|
|
AckMessage,
|
|
|
|
IdResponseMessage,
|
|
|
|
TextMessage,
|
|
|
|
} from "./lib/ServerMessage";
|
2022-01-08 17:17:43 +01:00
|
|
|
|
2022-01-07 17:36:19 +01:00
|
|
|
import {
|
2022-01-07 18:59:25 +01:00
|
|
|
isServerMessage,
|
|
|
|
isTextMessage,
|
|
|
|
MessageType,
|
2022-01-08 17:17:43 +01:00
|
|
|
} from "./lib/ServerMessage";
|
|
|
|
|
|
|
|
const port = 8085;
|
2022-01-07 17:36:19 +01:00
|
|
|
const timeout = 15000;
|
|
|
|
|
2022-01-08 17:17:43 +01:00
|
|
|
const httpsServer = https.createServer({
|
|
|
|
key: fs.readFileSync("./key.pem"),
|
|
|
|
cert: fs.readFileSync("./cert.pem"),
|
|
|
|
});
|
|
|
|
const webSocketServer = new WebSocketServer({ server: httpsServer });
|
2022-01-07 17:36:19 +01:00
|
|
|
|
|
|
|
console.log("listening on port: " + port);
|
|
|
|
|
2022-01-08 17:17:43 +01:00
|
|
|
async function handleTextMessage(message: TextMessage, from: string) {
|
|
|
|
for (const to of webSocketServer.clients) {
|
|
|
|
to.send(
|
|
|
|
JSON.stringify(
|
|
|
|
Object.assign({}, message, {
|
|
|
|
author: from,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
2022-01-07 17:36:19 +01:00
|
|
|
}
|
2022-01-08 17:17:43 +01:00
|
|
|
}
|
2022-01-07 17:36:19 +01:00
|
|
|
|
2022-01-08 20:23:03 +01:00
|
|
|
async function handleCloseConnection(id: string) {
|
|
|
|
activeConnections.delete(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
const activeConnections = new Set<string>();
|
|
|
|
|
|
|
|
webSocketServer.on("connection", function connection(socket) {
|
2022-01-08 17:17:43 +01:00
|
|
|
const close = (reason: string, code: number = 1000) => {
|
|
|
|
socket.send(
|
|
|
|
JSON.stringify({
|
|
|
|
type: MessageType.ACK,
|
|
|
|
date: Date.now(),
|
|
|
|
__ctx: `closing connection. reason: ${reason}`,
|
|
|
|
} as AckMessage)
|
|
|
|
);
|
|
|
|
socket.close(code, `closing connection. reason: ${reason}`);
|
2022-01-08 20:23:03 +01:00
|
|
|
handleCloseConnection(authorId);
|
2022-01-08 17:17:43 +01:00
|
|
|
};
|
2022-01-07 18:59:25 +01:00
|
|
|
|
2022-01-14 13:33:03 +01:00
|
|
|
const authorId: string = crypto.randomUUID();
|
2022-01-08 17:17:43 +01:00
|
|
|
|
|
|
|
socket.on("message", function (rawMessage: string) {
|
2022-01-07 18:59:25 +01:00
|
|
|
const message = JSON.parse(rawMessage);
|
|
|
|
if (!isServerMessage(message)) {
|
2022-01-14 13:33:03 +01:00
|
|
|
console.error(`Unexpected message received from client "${authorId}": \`${message}\``);
|
2022-01-08 20:23:03 +01:00
|
|
|
return;
|
2022-01-07 18:59:25 +01:00
|
|
|
}
|
2022-01-08 20:23:03 +01:00
|
|
|
|
2022-01-07 18:59:25 +01:00
|
|
|
if (isTextMessage(message)) {
|
2022-01-08 20:23:03 +01:00
|
|
|
if (message.author === authorId) {
|
|
|
|
handleTextMessage(message, authorId);
|
|
|
|
}
|
2022-01-07 18:59:25 +01:00
|
|
|
}
|
2022-01-08 20:23:03 +01:00
|
|
|
|
2022-01-07 17:36:19 +01:00
|
|
|
clearTimeout(closeTimeout);
|
|
|
|
closeTimeout = setTimeout(close, timeout);
|
|
|
|
});
|
|
|
|
socket.on("close", function close() {
|
|
|
|
console.log("closed a connection");
|
|
|
|
});
|
|
|
|
|
2022-01-08 20:23:03 +01:00
|
|
|
console.log("new client connected! ID:", authorId);
|
2022-01-08 17:17:43 +01:00
|
|
|
socket.send(
|
|
|
|
JSON.stringify({
|
2022-01-08 20:23:03 +01:00
|
|
|
type: MessageType.ID_RESPONSE,
|
2022-01-08 17:17:43 +01:00
|
|
|
__ctx: "connected successfully",
|
2022-01-08 20:23:03 +01:00
|
|
|
date: Date.now(),
|
|
|
|
authorId,
|
|
|
|
} as IdResponseMessage)
|
2022-01-08 17:17:43 +01:00
|
|
|
);
|
2022-01-08 20:23:03 +01:00
|
|
|
activeConnections.add(authorId);
|
2022-01-07 17:36:19 +01:00
|
|
|
let closeTimeout = setTimeout(close, timeout);
|
|
|
|
});
|
2022-01-08 17:17:43 +01:00
|
|
|
|
|
|
|
httpsServer.listen(port);
|