edgeport - v1.0.3
    Preparing search index...

    Function connect

    • Opens a WebSocket connection from a Cloudflare Worker.

      Performs the outbound client handshake with a fetch upgrade (the runtime handles the wss TLS, RFC 6455 framing, and client masking), accepts the returned socket, then yields a WsConnection you async-iterate for inbound messages. Offered subprotocols are sent as a comma-joined Sec-WebSocket-Protocol header; any opts.headers are merged in.

      Parameters

      • url: string

        The ws:// or wss:// endpoint to dial.

      • opts: WsConnectOptions = {}

        Optional subprotocols and extra request headers.

      Returns Promise<WsConnection>

      The connected WsConnection.

      If the server does not complete the upgrade or fetch fails.

      1.0.0

      import { connect } from 'edgeport/ws';

      const ws = await connect('wss://echo.example.com', { protocols: ['chat'] });
      ws.send('ping');
      for await (const msg of ws) {
      if (msg.type === 'text') console.log('reply:', msg.data);
      break;
      }
      ws.close(1000, 'done');
      const { code } = await ws.closed;
      console.log('closed with', code);