interface Deno.UpgradeWebSocketOptions
Options which can be set when performing a
Deno.upgradeWebSocket upgrade of a Request
Properties #
Sets the .protocol property on the client side web socket to the
value provided here, which should be one of the strings specified in the
protocols parameter when requesting the web socket. This is intended
for clients and servers to specify sub-protocols to use to communicate to
each other.
#idleTimeout: number If the client does not respond to this frame with a
pong within the timeout specified, the connection is deemed
unhealthy and is closed. The close and error event will be emitted.
The unit is seconds, with a default of 30.
Set to 0 to disable timeouts.
A node:net Socket from a node:http server's "upgrade" event.
When provided, the WebSocket upgrade is performed over this existing
TCP connection instead of through Deno.serve's built-in upgrade
mechanism. The 101 Switching Protocols response is written
automatically.
import http from "node:http";
const server = http.createServer();
server.on("upgrade", (req, socket, head) => {
const { socket: ws } = Deno.upgradeWebSocket(
new Request(`http://${req.headers.host}/`, {
headers: req.headers as HeadersInit,
}),
{ socket: socket as import("node:net").Socket, head },
);
ws.onmessage = (e) => ws.send(e.data);
});