Use lib
This commit is contained in:
parent
bad2e95d49
commit
a56d0e0f52
3 changed files with 26 additions and 18 deletions
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
[submodule "lib"]
|
||||||
|
path = lib
|
||||||
|
url = https://github.com/Toby222/web-drs-lib.git
|
1
lib
Submodule
1
lib
Submodule
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 9fc4e8f80d335abc3ab02bfc24d64afcc91f3567
|
|
@ -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);
|
||||||
socket.send(
|
|
||||||
JSON.stringify({
|
const message = JSON.parse(rawMessage);
|
||||||
date: Date.now(),
|
if (!isServerMessage(message)) {
|
||||||
author: "ECHO Service",
|
console.error(`Unexpected message received from client \`${message}\``);
|
||||||
content: message,
|
}
|
||||||
})
|
if (isTextMessage(message)) {
|
||||||
);
|
socket.send(
|
||||||
|
JSON.stringify({
|
||||||
|
type: MessageType.TEXT,
|
||||||
|
date: Date.now(),
|
||||||
|
author: "ECHO Service",
|
||||||
|
content: message.content,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
clearTimeout(closeTimeout);
|
clearTimeout(closeTimeout);
|
||||||
closeTimeout = setTimeout(close, timeout);
|
closeTimeout = setTimeout(close, timeout);
|
||||||
});
|
});
|
||||||
|
|
Reference in a new issue