1
Fork 0
This commit is contained in:
Tobias Berger 2022-01-07 18:59:25 +01:00
parent bad2e95d49
commit a56d0e0f52
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
3 changed files with 26 additions and 18 deletions

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "lib"]
path = lib
url = https://github.com/Toby222/web-drs-lib.git

1
lib Submodule

@ -0,0 +1 @@
Subproject commit 9fc4e8f80d335abc3ab02bfc24d64afcc91f3567

View file

@ -1,6 +1,9 @@
import { WebSocketServer } from "https://deno.land/x/websocket@v0.1.3/mod.ts";
import { import {
WebSocketServer isServerMessage,
} from "https://deno.land/x/websocket@v0.1.3/mod.ts"; isTextMessage,
MessageType,
} from "./lib/types/ServerMessage.ts";
const port = 8989; const port = 8989;
const timeout = 15000; const timeout = 15000;
@ -9,28 +12,29 @@ const server = new WebSocketServer(port);
console.log("listening on port: " + port); console.log("listening on port: " + port);
function ackPkg() {
return JSON.stringify({
type: "ack",
date: Date.now(),
});
}
server.on("connection", function connection(socket) { server.on("connection", function connection(socket) {
function close() { function close() {
socket.send("closing connection due to inactivity"); socket.send("closing connection due to inactivity");
socket.close(); socket.close();
} }
socket.on("message", function (message: string) { socket.on("message", function (rawMessage: string) {
console.log("message: " + message); 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( socket.send(
JSON.stringify({ JSON.stringify({
type: MessageType.TEXT,
date: Date.now(), date: Date.now(),
author: "ECHO Service", author: "ECHO Service",
content: message, content: message.content,
}) }),
); );
}
clearTimeout(closeTimeout); clearTimeout(closeTimeout);
closeTimeout = setTimeout(close, timeout); closeTimeout = setTimeout(close, timeout);
}); });