温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/reference/bun/WebSocketHandler/drain
点击访问原文链接

WebSocketHandler.drain method | Bun module | Bun

WebSocketHandler.drain method | Bun module | BunBuildDocsReferenceGuidesBlogDiscord/Bun/WebSocketHandler/drainMdrain

Search the reference...

/

BuildDocsReferenceGuidesBlogDiscord/Bun/WebSocketHandler/drainMdrain

method

WebSocketHandler.draindrain(ws: ServerWebSocketT>): void | Promisevoid>;

Called when a connection was previously under backpressure, meaning it had too many queued messages, but is now ready to receive more data.

@param ws

The websocket that is ready for more data

Referenced typesinterface ServerWebSocketT = undefined>

A fast WebSocket designed for servers.

Features:

Message compression - Messages can be compressedBackpressure - If the client is not ready to receive data, the server will tell you.Dropped messages - If the client cannot receive data, the server will tell you.Topics - Messages can be ServerWebSocket.published to a specific topic and the client can ServerWebSocket.subscribe to topics

This is slightly different than the browser WebSocket which Bun supports for clients.

Powered by uWebSockets.

Bun.serve({
websocket: {
open(ws) {
console.log("Connected", ws.remoteAddress);
},
message(ws, data) {
console.log("Received", data);
ws.send(data);
},
close(ws, code, reason) {
console.log("Disconnected", code, reason);
},
}
});
binaryType?: 'arraybuffer' | 'uint8array' | 'nodebuffer'

Sets how binary data is returned in events.

if nodebuffer, binary data is returned as Buffer objects. (default)if arraybuffer, binary data is returned as ArrayBuffer objects.if uint8array, binary data is returned as Uint8Array objects.
let ws: WebSocket;
ws.binaryType = "uint8array";
ws.addEventListener("message", ({ data }) => {
console.log(data instanceof Uint8Array); // true
});
data: T

Custom data that you can assign to a client, can be read and written at any time.

import { serve } from "bun";

serve({
fetch(request, server) {
const data = {
accessToken: request.headers.get("Authorization"),
};
if (server.upgrade(request, { data })) {
return;
}
return new Response();
},
websocket: {
data: {} as {accessToken: string | null},
message(ws) {
console.log(ws.data.accessToken);
}
}
});
readonly readyState: WebSocketReadyState

The ready state of the client.

if 0, the client is connecting.if 1, the client is connected.if 2, the client is closing.if 3, the client is closed.
console.log(socket.readyState); // 1
readonly remoteAddress: string

The IP address of the client.

console.log(socket.remoteAddress); // "127.0.0.1"
readonly subscriptions: string[]

Returns an array of all topics the client is currently subscribed to.

ws.subscribe("chat");
ws.subscribe("notifications");
console.log(ws.subscriptions); // ["chat", "notifications"]
close(code?: number,reason?: string): void;

Closes the connection.

Here is a list of close codes:

1000 means "normal closure" (default)1009 means a message was too big and was rejected1011 means the server encountered an error1012 means the server is restarting1013 means the server is too busy or the client is rate-limited4000 through 4999 are reserved for applications (you can use it!)

To close the connection abruptly, use terminate().

@param code

The close code to send

@param reason

The close reason to send

corkT = unknown>(callback: (ws: ServerWebSocketT>) => T): T;

Batches send() and publish() operations, which makes it faster to send data.

The message, open, and drain callbacks are automatically corked, so you only need to call this if you are sending messages outside of those callbacks or in async functions.

@param callback

The callback to run.

ws.cork((ctx) => {
ctx.send("These messages");
ctx.sendText("are sent");
ctx.sendBinary(new TextEncoder().encode("together!"));
});
getBufferedAmount(): number;isSubscribed(topic: string): boolean;

Is the client subscribed to a topic?

@param topic

The topic name.

ws.subscribe("chat");
console.log(ws.isSubscribed("chat")); // true
ping(data?: string | BufferSource): number;

Sends a ping.

@param data

The data to send

pong(data?: string | BufferSource): number;

Sends a pong.

@param data

The data to send

publish(topic: string,data: string | BufferSource,compress?: boolean): number;

Sends a message to subscribers of the topic.

@param topic

The topic name.

@param data

The data to send.

@param compress

Should the data be compressed? If the client does not support compression, this is ignored.

ws.publish("chat", "Hello!");
ws.publish("chat", "Compress this.", true);
ws.publish("chat", new Uint8Array([1, 2, 3, 4]));
publishBinary(topic: string,data: BufferSource,compress?: boolean): number;

Sends a binary message to subscribers of the topic.

@param topic

The topic name.

@param data

The data to send.

@param compress

Should the data be compressed? If the client does not support compression, this is ignored.

ws.publish("chat", new TextEncoder().encode("Hello!"));
ws.publish("chat", new Uint8Array([1, 2, 3, 4]), true);
publishText(topic: string,data: string,compress?: boolean): number;

Sends a text message to subscribers of the topic.

@param topic

The topic name.

@param data

The data to send.

@param compress

Should the data be compressed? If the client does not support compression, this is ignored.

ws.publish("chat", "Hello!");
ws.publish("chat", "Compress this.", true);
send(data: string | BufferSource,compress?: boolean): number;

Sends a message to the client.

@param data

The data to send.

@param compress

Should the data be compressed? If the client does not support compression, this is ignored.

ws.send("Hello!");
ws.send("Compress this.", true);
ws.send(new Uint8Array([1, 2, 3, 4]));
sendBinary(data: BufferSource,compress?: boolean): number;

Sends a binary message to the client.

@param data

The data to send.

@param compress

Should the data be compressed? If the client does not support compression, this is ignored.

ws.send(new TextEncoder().encode("Hello!"));
ws.send(new Uint8Array([1, 2, 3, 4]), true);
sendText(data: string,compress?: boolean): number;

Sends a text message to the client.

@param data

The data to send.

@param compress

Should the data be compressed? If the client does not support compression, this is ignored.

ws.send("Hello!");
ws.send("Compress this.", true);
subscribe(topic: string): void;

Subscribes a client to the topic.

@param topic

The topic name.

ws.subscribe("chat");
terminate(): void;

Abruptly close the connection.

To gracefully close the connection, use close().

unsubscribe(topic: string): void;

Unsubscribes a client to the topic.

@param topic

The topic name.

ws.unsubscribe("chat");

Resources

ReferenceDocsGuidesDiscordMerch StoreGitHubBlog 

Toolkit

RuntimePackage managerTest runnerBundlerPackage runner

Project

Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicense

Baked with ❤️ in San Francisco

We're hiring →

WebSocketHandler.drain method | Bun module | Bun,AI智能索引,全网链接索引,智能导航,网页索引

    API documentation for method bun.WebSocketHandler.drain | Bun