From bad2e95d499b9679fc26ee9d04b4c454f10ff454 Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Fri, 7 Jan 2022 17:36:19 +0100 Subject: [PATCH] Initial commit --- .gitignore | 3 +++ .vscode/settings.json | 7 +++++++ server.deno.ts | 44 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/settings.json create mode 100644 server.deno.ts diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9907591 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +// compiled binaries +*.deno +*.exe \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..fd58e75 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "deno.enable": true, + "deno.lint": true, + "deno.codeLens.implementations": true, + "deno.codeLens.references": true, + "deno.codeLens.referencesAllFunctions": true +} \ No newline at end of file diff --git a/server.deno.ts b/server.deno.ts new file mode 100644 index 0000000..0806703 --- /dev/null +++ b/server.deno.ts @@ -0,0 +1,44 @@ +import { + WebSocketServer +} from "https://deno.land/x/websocket@v0.1.3/mod.ts"; + +const port = 8989; +const timeout = 15000; + +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, + }) + ); + clearTimeout(closeTimeout); + closeTimeout = setTimeout(close, timeout); + }); + socket.on("close", function close() { + console.log("closed a connection"); + }); + + console.log("new client connected!"); + socket.send("connected"); + let closeTimeout = setTimeout(close, timeout); +});