2019-05-20 09:22:15 -04:00
|
|
|
# ws
|
|
|
|
|
2020-09-09 15:33:38 -04:00
|
|
|
ws module is made to provide helpers to create WebSocket server. For client
|
|
|
|
WebSockets, use the
|
|
|
|
[WebSocket API](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API).
|
2019-05-20 09:22:15 -04:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
```ts
|
2020-06-04 15:13:33 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-10-04 08:18:36 -04:00
|
|
|
import { serve } from "https://deno.land/std@$STD_VERSION/http/server.ts";
|
2019-05-20 09:22:15 -04:00
|
|
|
import {
|
|
|
|
acceptWebSocket,
|
|
|
|
isWebSocketCloseEvent,
|
|
|
|
isWebSocketPingEvent,
|
2020-06-04 15:13:33 -04:00
|
|
|
WebSocket,
|
2020-10-04 08:18:36 -04:00
|
|
|
} from "https://deno.land/std@$STD_VERSION/ws/mod.ts";
|
2020-05-04 12:27:06 -04:00
|
|
|
|
2020-06-04 15:13:33 -04:00
|
|
|
async function handleWs(sock: WebSocket) {
|
|
|
|
console.log("socket connected!");
|
2020-05-04 12:27:06 -04:00
|
|
|
try {
|
2020-06-04 15:13:33 -04:00
|
|
|
for await (const ev of sock) {
|
|
|
|
if (typeof ev === "string") {
|
2020-10-03 16:19:11 -04:00
|
|
|
// text message.
|
2020-06-04 15:13:33 -04:00
|
|
|
console.log("ws:Text", ev);
|
|
|
|
await sock.send(ev);
|
|
|
|
} else if (ev instanceof Uint8Array) {
|
2020-10-03 16:19:11 -04:00
|
|
|
// binary message.
|
2020-06-04 15:13:33 -04:00
|
|
|
console.log("ws:Binary", ev);
|
|
|
|
} else if (isWebSocketPingEvent(ev)) {
|
|
|
|
const [, body] = ev;
|
2020-10-03 16:19:11 -04:00
|
|
|
// ping.
|
2020-06-04 15:13:33 -04:00
|
|
|
console.log("ws:Ping", body);
|
|
|
|
} else if (isWebSocketCloseEvent(ev)) {
|
2020-10-03 16:19:11 -04:00
|
|
|
// close.
|
2020-06-04 15:13:33 -04:00
|
|
|
const { code, reason } = ev;
|
|
|
|
console.log("ws:Close", code, reason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`failed to receive frame: ${err}`);
|
|
|
|
|
|
|
|
if (!sock.isClosed) {
|
|
|
|
await sock.close(1000).catch(console.error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (import.meta.main) {
|
|
|
|
/** websocket echo server */
|
|
|
|
const port = Deno.args[0] || "8080";
|
|
|
|
console.log(`websocket server is running on :${port}`);
|
|
|
|
for await (const req of serve(`:${port}`)) {
|
|
|
|
const { conn, r: bufReader, w: bufWriter, headers } = req;
|
|
|
|
acceptWebSocket({
|
2020-05-04 12:27:06 -04:00
|
|
|
conn,
|
|
|
|
bufReader,
|
|
|
|
bufWriter,
|
|
|
|
headers,
|
2020-06-04 15:13:33 -04:00
|
|
|
})
|
|
|
|
.then(handleWs)
|
2020-06-10 09:03:26 -04:00
|
|
|
.catch(async (err) => {
|
2020-06-04 15:13:33 -04:00
|
|
|
console.error(`failed to accept websocket: ${err}`);
|
|
|
|
await req.respond({ status: 400 });
|
|
|
|
});
|
2020-05-04 12:27:06 -04:00
|
|
|
}
|
2019-05-20 09:22:15 -04:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## API
|
|
|
|
|
|
|
|
### isWebSocketCloseEvent
|
|
|
|
|
|
|
|
Returns true if input value is a WebSocketCloseEvent, false otherwise.
|
|
|
|
|
|
|
|
### isWebSocketPingEvent
|
|
|
|
|
|
|
|
Returns true if input value is a WebSocketPingEvent, false otherwise.
|
|
|
|
|
|
|
|
### isWebSocketPongEvent
|
|
|
|
|
|
|
|
Returns true if input value is a WebSocketPongEvent, false otherwise.
|
|
|
|
|
|
|
|
### unmask
|
|
|
|
|
|
|
|
Unmask masked WebSocket payload.
|
|
|
|
|
|
|
|
### writeFrame
|
|
|
|
|
|
|
|
Write WebSocket frame to inputted writer.
|
|
|
|
|
|
|
|
### readFrame
|
|
|
|
|
|
|
|
Read WebSocket frame from inputted BufReader.
|
|
|
|
|
|
|
|
### createMask
|
|
|
|
|
|
|
|
Create mask from the client to the server with random 32bit number.
|
|
|
|
|
|
|
|
### acceptable
|
|
|
|
|
2020-10-03 16:19:11 -04:00
|
|
|
Returns true if input headers are usable for WebSocket, otherwise false.
|
2019-05-20 09:22:15 -04:00
|
|
|
|
|
|
|
### createSecAccept
|
|
|
|
|
|
|
|
Create value of Sec-WebSocket-Accept header from inputted nonce.
|
|
|
|
|
|
|
|
### acceptWebSocket
|
|
|
|
|
|
|
|
Upgrade inputted TCP connection into WebSocket connection.
|
|
|
|
|
|
|
|
### createSecKey
|
|
|
|
|
|
|
|
Returns base64 encoded 16 bytes string for Sec-WebSocket-Key header.
|