mirror of
https://github.com/denoland/deno.git
synced 2024-11-27 16:10:57 -05:00
d29957ad17
Original: 0fc13fffbd
39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
import { serve } from "https://deno.land/std/http/mod.ts";
|
|
import {
|
|
acceptWebSocket,
|
|
isWebSocketCloseEvent,
|
|
isWebSocketPingEvent
|
|
} from "https://deno.land/std/ws/mod.ts";
|
|
|
|
async function main(): Promise<void> {
|
|
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();
|