summaryrefslogtreecommitdiff
path: root/examples/ws.ts
blob: f8e711c49b188285ac5d49c3b2bf568bbc5a910a (plain)
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
32
33
34
35
36
37
38
39
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { serve } from "https://deno.land/x/net/http.ts";
import {
  acceptWebSocket,
  isWebSocketCloseEvent,
  isWebSocketPingEvent
} from "https://deno.land/x/net/ws.ts";

async function main() {
  console.log("websocket server is running on 0.0.0.0:8080");
  for await (const req of serve("0.0.0.0:8080")) {
    if (req.url === "/ws") {
      (async () => {
        const sock = await acceptWebSocket(req);
        console.log("socket connected!");
        for await (const ev of sock.receive()) {
          if (typeof ev === "string") {
            // text message
            console.log("ws:Text", ev);
            await sock.send(ev);
          } else if (ev instanceof Uint8Array) {
            // binary message
            console.log("ws:Binary", ev);
          } else if (isWebSocketPingEvent(ev)) {
            const [_, body] = ev;
            // ping
            console.log("ws:Ping", body);
          } else if (isWebSocketCloseEvent(ev)) {
            // close
            const { code, reason } = ev;
            console.log("ws:Close", code, reason);
          }
        }
      })();
    }
  }
}

main();