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 {
|
||||
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);
|
||||
});
|
||||
|
|
Reference in a new issue