Closed leeian1011 closed 1 month ago
uWS.js Version: v20.44.0 node version: v20.5.1
Description: When attempting to retrieve the UserData generic from the WebSocket class by calling the getUserData() method, the returned value is the UserData type but the WebSocket type.
UserData
WebSocket
getUserData()
Simple reproducible example:
import { App, DISABLED, HttpRequest, TemplatedApp, WebSocket, HttpResponse, us_socket_context_t } from "uWebSockets.js"; class WebSocketServer { private readonly _websocket: TemplatedApp constructor() { this._websocket = App().ws('/*', { upgrade(res: HttpResponse, req: HttpRequest, context: us_socket_context_t) { console.log(req) console.log(req.getHeader('x-forwarded-for')) res.upgrade( { req }, req.getHeader('sec-websocket-key'), req.getHeader('sec-websocket-protocol'), req.getHeader('sec-websocket-extensions'), context ) }, message(ws: WebSocket<HttpRequest>, message) { const req = ws.getUserData() console.log(req) console.log(req.getHeader('x-forwarded-for')) }, }) } public async start(port: number) { await new Promise<void>((resolve, reject) => { try { this._websocket.listen(port, (socket) => { if (socket) { resolve(); } else { console.error("Failed to setup socket for listening.") reject("Failed to setup socket for listening."); } }) } catch (e) { reject(e) } }).catch((e) => { console.error(`Caught Execption ${e}`) }) } } const server = new WebSocketServer() await server.start(7000)
Expected result:
uWS.HttpRequest {} 127.0.0.1 uWS.HttpRequest {} 127.0.0.1
Actual result:
You can't pass along HttpRequest either way. You can look at Upgrade example
https://github.com/uNetworking/uWebSockets.js/blob/master/examples/Upgrade.js
I see, this has helped. Thank you
uWS.js Version: v20.44.0 node version: v20.5.1
Description: When attempting to retrieve the
UserData
generic from theWebSocket
class by calling thegetUserData()
method, the returned value is theUserData
type but theWebSocket
type.Simple reproducible example:
Expected result:
Actual result: