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 {
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.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: message.content,
}),
);
}
clearTimeout(closeTimeout);
closeTimeout = setTimeout(close, timeout);
});