1

在基于 uWebSocket.js 的服务器上,我使用的是参数化路径,例如/socket/:name.

如何捕获使用的实际路径?

例如,如果套接字连接到/socket/one,我希望能够提取one字符串,或者至少获取路径以便我可以手动提取路径参数。

4

1 回答 1

1

根据此评论想通了。在此处复制粘贴解决方案。您需要使用 ws 升级处理程序来传递来自原始 http 请求的值:

// pass values from ws upgrade handler
uws.App().ws('/', {
   upgrade: (res, req, context) => {
      res.upgrade(
         { ip: res.getRemoteAddressAsText() }, // 1st argument sets which properties to pass to ws object, in this case ip address
         req.getHeader('sec-websocket-key'),
         req.getHeader('sec-websocket-protocol'),
         req.getHeader('sec-websocket-extensions'), // 3 headers are used to setup websocket
         context // also used to setup websocket
      )
   },
   open: ws => {
      console.log(ws.ip)
   }
})
于 2021-04-05T14:35:28.617 回答