-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
31 lines (28 loc) · 804 Bytes
/
app.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
29
30
31
import { serve } from "https://deno.land/std/http/server.ts";
import { acceptWebSocket, acceptable } from "https://deno.land/std/ws/mod.ts";
import { chatConnection } from './ws/chatroom.ts';
// setting up the server
const server = serve({ port: 3000 });
console.log("http://localhost:3000/");
// async forloop
for await (const req of server) {
// serve index page
if (req.url === '/') {
req.respond({
status: 200,
body: await Deno.open('./public/index.html')
});
}
// accept websocket connection
if (req.url === '/ws') { //we specified this path in the client-side
if (acceptable(req)) {
acceptWebSocket({
conn: req.conn,
bufReader: req.r,
bufWriter: req.w,
headers: req.headers
})
.then(chatConnection);
}
}
}