1
Fork 0

Use uuid for authorId

This commit is contained in:
Tobias Berger 2022-01-14 13:33:03 +01:00
parent e00b0372ee
commit 56ef698274

View file

@ -16,8 +16,6 @@ import {
MessageType,
} from "./lib/ServerMessage";
const SALT = crypto.randomBytes(2048).toString("hex");
const port = 8085;
const timeout = 15000;
@ -29,10 +27,6 @@ const webSocketServer = new WebSocketServer({ server: httpsServer });
console.log("listening on port: " + port);
function hash(str: string) {
return crypto.createHash("SHAKE256").update(str, "utf-8").digest("hex");
}
async function handleTextMessage(message: TextMessage, from: string) {
for (const to of webSocketServer.clients) {
to.send(
@ -50,7 +44,6 @@ async function handleCloseConnection(id: string) {
}
const activeConnections = new Set<string>();
let nextId = 0;
webSocketServer.on("connection", function connection(socket) {
const close = (reason: string, code: number = 1000) => {
@ -65,12 +58,12 @@ webSocketServer.on("connection", function connection(socket) {
handleCloseConnection(authorId);
};
const authorId: string = hash(nextId++ + SALT);
const authorId: string = crypto.randomUUID();
socket.on("message", function (rawMessage: string) {
const message = JSON.parse(rawMessage);
if (!isServerMessage(message)) {
console.error(`Unexpected message received from client \`${message}\``);
console.error(`Unexpected message received from client "${authorId}": \`${message}\``);
return;
}