1
Fork 0

User:Name handling

This commit is contained in:
Tobias Berger 2022-01-21 14:51:26 +01:00
parent 4d7ab58c0d
commit 9db0f98430
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
2 changed files with 71 additions and 6 deletions

2
lib

@ -1 +1 @@
Subproject commit a957a29498d6b9c3b121f8e27e43ad73d9342075 Subproject commit a618eda73268a675e0c90dace9751ad05d5e0443

View file

@ -9,18 +9,22 @@ import type {
IdResponseMessage, IdResponseMessage,
TextMessage, TextMessage,
TypingMessage, TypingMessage,
ConnectedUser,
ConnectedUsersMessage,
} from "./lib/ServerMessage"; } from "./lib/ServerMessage";
import { import {
isServerMessage, isServerMessage,
isTextMessage, isTextMessage,
isTypingMessage, isTypingMessage,
isDesiredNameMessage,
MessageType, MessageType,
} from "./lib/ServerMessage"; } from "./lib/ServerMessage";
const port = 8085; const port = 8085;
const timeout = 15000; const timeout = 5000;
const typingTimeout = 2000; const typingTimeout = 2000;
const serverId = "00000000-0000-0000-0000-000000000000";
const httpsServer = https.createServer({ const httpsServer = https.createServer({
key: fs.readFileSync("./key.pem"), key: fs.readFileSync("./key.pem"),
@ -49,6 +53,10 @@ async function handleTextMessage(message: TextMessage, from: string) {
const activeConnections = new Set<string>(); const activeConnections = new Set<string>();
const currentlyTyping = new Set<string>(); const currentlyTyping = new Set<string>();
const currentlyTypingTimeouts = new Map<string, ReturnType<typeof setTimeout>>(); const currentlyTypingTimeouts = new Map<string, ReturnType<typeof setTimeout>>();
const desiredNames = new Map<string, string>();
activeConnections.add(serverId);
desiredNames.set(serverId, "[SYSTEM]");
async function handleTypingMessage(_message: TypingMessage, from: string) { async function handleTypingMessage(_message: TypingMessage, from: string) {
currentlyTyping.add(from); currentlyTyping.add(from);
@ -85,6 +93,50 @@ async function sendCurrentlyTypingMessage(from?: string, stopped = false) {
async function handleCloseConnection(id: string) { async function handleCloseConnection(id: string) {
activeConnections.delete(id); activeConnections.delete(id);
if (desiredNames.has(id)) {
desiredNames.delete(id);
}
for (const to of webSocketServer.clients) {
to.send(
JSON.stringify({
type: MessageType.CONNECTED_USERS,
__ctx: `${id} left`,
date: Date.now(),
connected:
Array.from(activeConnections,
(id) => {
return {
id,
desiredName: desiredNames.get(id),
} as ConnectedUser;
}),
} as ConnectedUsersMessage)
);
}
}
function handleNewConnection(id: string) {
activeConnections.add(id);
for (const to of webSocketServer.clients) {
to.send(
JSON.stringify({
type: MessageType.CONNECTED_USERS,
__ctx: `${id} joined`,
date: Date.now(),
connected:
Array.from(activeConnections,
(id) => {
return {
id,
desiredName: desiredNames.get(id),
} as ConnectedUser;
}),
} as ConnectedUsersMessage)
);
}
} }
webSocketServer.on("connection", function connection(socket) { webSocketServer.on("connection", function connection(socket) {
@ -107,6 +159,9 @@ webSocketServer.on("connection", function connection(socket) {
if (!isServerMessage(message)) { if (!isServerMessage(message)) {
console.error(`Unexpected message received from client "${authorId}": \`${message}\``); console.error(`Unexpected message received from client "${authorId}": \`${message}\``);
return; return;
} else {
clearTimeout(closeTimeout);
closeTimeout = setTimeout(close, timeout);
} }
if (isTextMessage(message)) { if (isTextMessage(message)) {
@ -115,10 +170,9 @@ webSocketServer.on("connection", function connection(socket) {
} }
} else if (isTypingMessage(message)) { } else if (isTypingMessage(message)) {
handleTypingMessage(message, authorId); handleTypingMessage(message, authorId);
} else if (isDesiredNameMessage(message)) {
desiredNames.set(authorId, message.desiredName);
} }
clearTimeout(closeTimeout);
closeTimeout = setTimeout(close, timeout);
}); });
socket.on("close", function close() { socket.on("close", function close() {
console.log("closed a connection"); console.log("closed a connection");
@ -133,7 +187,18 @@ webSocketServer.on("connection", function connection(socket) {
authorId, authorId,
} as IdResponseMessage) } as IdResponseMessage)
); );
activeConnections.add(authorId);
handleNewConnection(authorId);
socket.send(
JSON.stringify({
type: MessageType.TEXT,
__ctx: "server welcome message",
date: Date.now(),
author: serverId,
content: "Successfully connected. Welcome!\nSend a message to talk to other connected users.",
} as TextMessage)
);
let closeTimeout = setTimeout(close, timeout); let closeTimeout = setTimeout(close, timeout);
}); });