unjs / crossws

🔌 Cross-platform WebSocket Servers for Node.js, Deno, Bun and Cloudflare Workers.
https://crossws.unjs.io
Other
394 stars 17 forks source link

Open hook never called when route != '/' since peer as no url (v 0.3.0) #78

Closed Dodobibi closed 2 months ago

Dodobibi commented 2 months ago

Environment

Reproduction

Make a nuxt project

Define this handler in a route like server/api/testws

export default eventHandler({

  handler: (event) => {
    // Rest handler
  },

  websocket: defineHooks({
    upgrade(req) {
      console.log(`[ws] upgrading ${req.url}...`);
    },
    open(peer) {
      console.log(`[ws] open: ${peer}`);
    },
    message(peer, message) {
      console.log('[ws] message', peer, message);
      if (message.text().includes('ping')) {
        peer.send('pong');
      }
    },
    close(peer, event) {
      console.log('[ws] close', peer, event);
    },
    error(peer, error) {
      console.log('[ws] error', peer, error);
    },
  }),
});

Describe the bug

Upgrade hook is called, but Open is not.

It seems than Peer have not url property, then, resolver fallback (after upgradde) from wrong hooks (at /) instead of hooks defined at the right route /api/testws in the reproduction exemple.

hooks.callHook("open", peer); ==> const resolveHooksPromise = this.options.resolve?.(arg1);

Additional context

No response

Logs

No response

pi0 commented 2 months ago

nitro is not upgraded yet..

Dodobibi commented 2 months ago

Ah, Ok, thanks 👍