async function connectWebSocket(endpoint, headers = new Headers()) {
  const url = new URL(endpoint);
  const { hostname } = url;
  let conn;
  if (url.protocol === "http:" || url.protocol === "ws:") {
    const port = parseInt(url.port || "80");
    conn = await Deno.connect({
      hostname,
      port,
    });
  } else if (url.protocol === "https:" || url.protocol === "wss:") {
    const port = parseInt(url.port || "443");
    conn = await Deno.connectTls({
      hostname,
      port,
    });
  } else {
    throw new Error("ws: unsupported protocol: " + url.protocol);
  }
  const bufWriter = new BufWriter(conn);
  const bufReader = new BufReader(conn);
  try {
    await handshake(url, headers, bufReader, bufWriter);
  } catch (err) {
    conn.close();
    throw err;
  }
  return new WebSocketImpl({
    conn,
    bufWriter,
    bufReader,
    mask: createMask(),
  });
}
/**
 * Connect to given websocket endpoint url.
 * Endpoint must be acceptable for URL.
 */
Connect to given websocket endpoint url.
Endpoint must be acceptable for URL.
Connect to given websocket endpoint url . Endpoint must be acceptable for URL .
async function connectWebSocket(endpoint, headers = new Headers()) {
  const url = new URL(endpoint);
  const { hostname } = url;
  let conn;
  if (url.protocol === "http:" || url.protocol === "ws:") {
    const port = parseInt(url.port || "80");
    conn = await Deno.connect({
      hostname,
      port,
    });
  } else if (url.protocol === "https:" || url.protocol === "wss:") {
    const port = parseInt(url.port || "443");
    conn = await Deno.connectTls({
      hostname,
      port,
    });
  } else {
    throw new Error("ws: unsupported protocol: " + url.protocol);
  }
  const bufWriter = new BufWriter(conn);
  const bufReader = new BufReader(conn);
  try {
    await handshake(url, headers, bufReader, bufWriter);
  } catch (err) {
    conn.close();
    throw err;
  }
  return new WebSocketImpl({
    conn,
    bufWriter,
    bufReader,
    mask: createMask(),
  });
}
async function connectWebSocket ( endpoint , headers = new Headers ( ) ) { const url = new URL ( endpoint ) ; const { hostname } = url ; let conn ; if ( url . protocol === "http:" || url . protocol === "ws:" ) { const port = parseInt ( url . port || "80" ) ; conn = await Deno . connect ( { hostname , port , } ) ; } else if ( url . protocol === "https:" || url . protocol === "wss:" ) { const port = parseInt ( url . port || "443" ) ; conn = await Deno . connectTls ( { hostname , port , } ) ; } else { throw new Error ( "ws: unsupported protocol: " + url . protocol ) ; } const bufWriter = new BufWriter ( conn ) ; const bufReader = new BufReader ( conn ) ; try { await handshake ( url , headers , bufReader , bufWriter ) ; } catch ( err ) { conn . close ( ) ; throw err ; } return new WebSocketImpl ( { conn , bufWriter , bufReader , mask : createMask ( ) , } ) ; }
Connect to given websocket endpoint url.
Connect to given websocket endpoint url .