From a56d0e0f520ff831ad81c51b78146be1724c976e Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Fri, 7 Jan 2022 18:59:25 +0100 Subject: [PATCH] Use lib --- .gitmodules | 3 +++ lib | 1 + server.deno.ts | 40 ++++++++++++++++++++++------------------ 3 files changed, 26 insertions(+), 18 deletions(-) create mode 100644 .gitmodules create mode 160000 lib diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..abb9340 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "lib"] + path = lib + url = https://github.com/Toby222/web-drs-lib.git diff --git a/lib b/lib new file mode 160000 index 0000000..9fc4e8f --- /dev/null +++ b/lib @@ -0,0 +1 @@ +Subproject commit 9fc4e8f80d335abc3ab02bfc24d64afcc91f3567 diff --git a/server.deno.ts b/server.deno.ts index 0806703..1a5105e 100644 --- a/server.deno.ts +++ b/server.deno.ts @@ -1,6 +1,9 @@ +import { WebSocketServer } from "https://deno.land/x/websocket@v0.1.3/mod.ts"; import { - WebSocketServer -} from "https://deno.land/x/websocket@v0.1.3/mod.ts"; + isServerMessage, + isTextMessage, + MessageType, +} from "./lib/types/ServerMessage.ts"; const port = 8989; const timeout = 15000; @@ -9,28 +12,29 @@ const server = new WebSocketServer(port); console.log("listening on port: " + port); -function ackPkg() { - return JSON.stringify({ - type: "ack", - date: Date.now(), - }); -} - server.on("connection", function connection(socket) { function close() { socket.send("closing connection due to inactivity"); socket.close(); } - socket.on("message", function (message: string) { - console.log("message: " + message); - socket.send( - JSON.stringify({ - date: Date.now(), - author: "ECHO Service", - content: message, - }) - ); + socket.on("message", function (rawMessage: string) { + console.log("message: " + rawMessage); + + const message = JSON.parse(rawMessage); + if (!isServerMessage(message)) { + console.error(`Unexpected message received from client \`${message}\``); + } + if (isTextMessage(message)) { + socket.send( + JSON.stringify({ + type: MessageType.TEXT, + date: Date.now(), + author: "ECHO Service", + content: message.content, + }), + ); + } clearTimeout(closeTimeout); closeTimeout = setTimeout(close, timeout); });