unjs / h3

⚡️ Minimal H(TTP) framework built for high performance and portability

Home Page:https://h3.unjs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mount websocket on same path as another handler

tobiasdiez opened this issue · comments

Environment

Nightly version of h3

Reproduction

Is there a template to start a reproduction?

Describe the bug

If you have a websocket handler and a normal handler on the same route, say

app.use('/', [
  defineWebSocketHandler({...}),
  defineEventHandler({...}),
])

or, the other way around,

app.use('/', [
  defineEventHandler({...}),
  defineWebSocketHandler({...}),
])

Then only the websocket or the normal request is working (depending on which comes first).

Additional context

No response

Logs

No response

This is how internally defineWebSocketHandler is implemented:

export function defineWebSocketHandler(hooks: Partial<WSHooks>) {
  return defineEventHandler({
    handler() {
      throw createError({
        statusCode: 426,
        statusMessage: "Upgrade Required",
      });
    },
    websocket: hooks,
  });
}

As you can see, it is a shortcut wrapper. You can directly use object syntax to mix:

  return defineEventHandler({
    websocket: hooks,
    handler() {
      // Your logic
    },
  });

Thanks, this seems to work well on non-root paths but the websocket listeners are not invoked when I use the root path (app.use('/', ...).

Is this a known bug or should I open a new report?