Begin tracking active connections
This commit is contained in:
parent
25a11f5761
commit
00ce8f1016
1 changed files with 27 additions and 26 deletions
|
@ -4,7 +4,11 @@ import { WebSocketServer } from "ws";
|
||||||
|
|
||||||
import * as crypto from "crypto";
|
import * as crypto from "crypto";
|
||||||
|
|
||||||
import type { AckMessage, TextMessage } from "./lib/ServerMessage";
|
import type {
|
||||||
|
AckMessage,
|
||||||
|
IdResponseMessage,
|
||||||
|
TextMessage,
|
||||||
|
} from "./lib/ServerMessage";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
isServerMessage,
|
isServerMessage,
|
||||||
|
@ -41,7 +45,14 @@ async function handleTextMessage(message: TextMessage, from: string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
webSocketServer.on("connection", function connection(socket, request) {
|
async function handleCloseConnection(id: string) {
|
||||||
|
activeConnections.delete(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
const activeConnections = new Set<string>();
|
||||||
|
let nextId = 0;
|
||||||
|
|
||||||
|
webSocketServer.on("connection", function connection(socket) {
|
||||||
const close = (reason: string, code: number = 1000) => {
|
const close = (reason: string, code: number = 1000) => {
|
||||||
socket.send(
|
socket.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
|
@ -51,36 +62,24 @@ webSocketServer.on("connection", function connection(socket, request) {
|
||||||
} as AckMessage)
|
} as AckMessage)
|
||||||
);
|
);
|
||||||
socket.close(code, `closing connection. reason: ${reason}`);
|
socket.close(code, `closing connection. reason: ${reason}`);
|
||||||
|
handleCloseConnection(authorId);
|
||||||
};
|
};
|
||||||
|
|
||||||
let authorID: string;
|
const authorId: string = hash(nextId++ + SALT);
|
||||||
{
|
|
||||||
let ip: string | undefined;
|
|
||||||
if (request.headers["x-forwarded-for"] !== undefined) {
|
|
||||||
const forwardedFor = request.headers["x-forwarded-for"];
|
|
||||||
ip =
|
|
||||||
typeof forwardedFor === "string"
|
|
||||||
? forwardedFor.split(",")[0]?.trim()
|
|
||||||
: forwardedFor[0];
|
|
||||||
} else {
|
|
||||||
ip = request.socket.remoteAddress;
|
|
||||||
}
|
|
||||||
if (ip === undefined) {
|
|
||||||
close("could not generate author id", 1008);
|
|
||||||
console.error("connection without IP. closing.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
authorID = hash(ip + SALT);
|
|
||||||
}
|
|
||||||
|
|
||||||
socket.on("message", function (rawMessage: string) {
|
socket.on("message", function (rawMessage: string) {
|
||||||
const message = JSON.parse(rawMessage);
|
const message = JSON.parse(rawMessage);
|
||||||
if (!isServerMessage(message)) {
|
if (!isServerMessage(message)) {
|
||||||
console.error(`Unexpected message received from client \`${message}\``);
|
console.error(`Unexpected message received from client \`${message}\``);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isTextMessage(message)) {
|
if (isTextMessage(message)) {
|
||||||
handleTextMessage(message, authorID);
|
if (message.author === authorId) {
|
||||||
|
handleTextMessage(message, authorId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
clearTimeout(closeTimeout);
|
clearTimeout(closeTimeout);
|
||||||
closeTimeout = setTimeout(close, timeout);
|
closeTimeout = setTimeout(close, timeout);
|
||||||
});
|
});
|
||||||
|
@ -88,14 +87,16 @@ webSocketServer.on("connection", function connection(socket, request) {
|
||||||
console.log("closed a connection");
|
console.log("closed a connection");
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("new client connected! ID:", authorID);
|
console.log("new client connected! ID:", authorId);
|
||||||
socket.send(
|
socket.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
type: MessageType.ACK,
|
type: MessageType.ID_RESPONSE,
|
||||||
date: Date.now(),
|
|
||||||
__ctx: "connected successfully",
|
__ctx: "connected successfully",
|
||||||
} as AckMessage)
|
date: Date.now(),
|
||||||
|
authorId,
|
||||||
|
} as IdResponseMessage)
|
||||||
);
|
);
|
||||||
|
activeConnections.add(authorId);
|
||||||
let closeTimeout = setTimeout(close, timeout);
|
let closeTimeout = setTimeout(close, timeout);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Reference in a new issue