-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
28 lines (25 loc) · 915 Bytes
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { serve } from "https://deno.land/std@0.95.0/http/server.ts";
import { serveFile } from "https://deno.land/std@0.95.0/http/file_server.ts";
import { Sono } from "https://deno.land/x/sono@v1.0/mod.ts";
const server = serve({ port: 3009 });
const sono = new Sono();
sono.channel("secret", () => {
console.log("secret opened");
});
for await (const req of server) {
if (req.method === "GET" && req.url === "/") {
const path = `${Deno.cwd()}/static/index.html`;
const content = await serveFile(req, path);
req.respond(content);
} else if (req.method === "GET" && req.url === "/ws") {
sono.connect(req, () => {
sono.emit("new client connected");
});
} else if (req.method === "GET" && req.url === "/favicon.ico") {
// Do nothing in case of favicon request
} else {
const path = `${Deno.cwd()}/static/${req.url}`;
const content = await serveFile(req, path);
req.respond(content);
}
}