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); });