Initial commit
This commit is contained in:
commit
bad2e95d49
3 changed files with 54 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
// compiled binaries
|
||||
*.deno
|
||||
*.exe
|
7
.vscode/settings.json
vendored
Normal file
7
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"deno.enable": true,
|
||||
"deno.lint": true,
|
||||
"deno.codeLens.implementations": true,
|
||||
"deno.codeLens.references": true,
|
||||
"deno.codeLens.referencesAllFunctions": true
|
||||
}
|
44
server.deno.ts
Normal file
44
server.deno.ts
Normal file
|
@ -0,0 +1,44 @@
|
|||
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);
|
||||
});
|
Reference in a new issue