Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First-class WebSocket server support #10363

Closed
MarkTiedemann opened this issue Apr 25, 2021 · 2 comments
Closed

First-class WebSocket server support #10363

MarkTiedemann opened this issue Apr 25, 2021 · 2 comments

Comments

@MarkTiedemann
Copy link
Contributor

I like the design of the new Deno.serveHttp API. I think a similar design would work well for WebSockets, too.

Example API:

async function acceptWebSocket(
  conn: Deno.Conn,
  httpConn: Deno.RequestEvent
): Promise<WebSocket> {}

Example usage:

for await (const conn of Deno.listen({ port: 80 })) {
  handleConn(conn);
}

async function handleConn(conn: Deno.Conn) {
  for await (const httpConn of Deno.serveHttp(conn)) {
    handleHttpConn(conn, httpConn);
  }
}

async function handleHttpConn(conn: Deno.Conn, httpConn: Deno.RequestEvent) {
  try {
    const ws = await Deno.acceptWebSocket(conn, httpConn);
    ws.onopen = todo;
    ws.onmessage = todo;
    ws.onerror = todo;
    ws.onclose = todo;
  } catch {
    httpConn.respondWith(new Response("Failed to accept WebSocket", { status: 400 }));
  }
}

function todo() {}
@crowlKats
Copy link
Member

Duplicate of #10211

@lucacasonato
Copy link
Member

Closing as duplicate of #10211

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants