diff options
Diffstat (limited to 'std/ws/mod.ts')
-rw-r--r-- | std/ws/mod.ts | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/std/ws/mod.ts b/std/ws/mod.ts index 7e444f274..2a18cded7 100644 --- a/std/ws/mod.ts +++ b/std/ws/mod.ts @@ -10,8 +10,10 @@ import { TextProtoReader } from "../textproto/mod.ts"; import { Deferred, deferred } from "../util/async.ts"; import { assert } from "../testing/asserts.ts"; import { concat } from "../bytes/mod.ts"; +import { copyBytes } from "../io/util.ts"; import Conn = Deno.Conn; import Writer = Deno.Writer; +import Reader = Deno.Reader; export enum OpCode { Continue = 0x0, @@ -65,7 +67,7 @@ export interface WebSocketFrame { payload: Uint8Array; } -export interface WebSocket { +export interface WebSocket extends Reader, Writer { readonly conn: Conn; readonly isClosed: boolean; @@ -327,6 +329,26 @@ class WebSocketImpl implements WebSocket { return this.enqueue(frame); } + async write(p: Uint8Array): Promise<number> { + await this.send(p); + + return p.byteLength; + } + + async read(p: Uint8Array): Promise<number | null> { + for await (const ev of this.receive()) { + if (ev instanceof Uint8Array) { + return copyBytes(p, ev); + } + + if (typeof ev === "string") { + return copyBytes(p, encode(ev)); + } + } + + return null; + } + ping(data: WebSocketMessage = ""): Promise<void> { const payload = typeof data === "string" ? encode(data) : data; const frame = { |