uNetworking / uWebSockets

Simple, secure & standards compliant web server for the most demanding of applications
Apache License 2.0
17.24k stars 1.75k forks source link

Remove getUserData #1160

Closed hellow554 closed 3 years ago

hellow554 commented 3 years ago

Currently getUserData returns a void* which must be casted manually to my type. But I will pass the type in the ws function, so my question is, would it be possible to pass that type internaly to the Websocket class as well, so we can get a function signature like this: T &getUserData()?

Would make it a little bit safer to use.

ghost commented 3 years ago

Yes definitely. But that would have to break backwards compatibility since WebSocket needs one more template argument. This can be considered for next major release.

ghost commented 3 years ago

Why not just remove getUserData and make the WebSocket derive from USERDATA then?

Then you can do things like

struct PerSocketData { int something = 12; };

ws->something++;

ghost commented 3 years ago

That would sync with the JavaScript wrapper as well, since they just do ws.something++; That removes differences between the two

ghost commented 3 years ago

getUserData remains but is now type safe. I did not find a good way to remove the function so it stays

hellow554 commented 3 years ago

https://github.com/uNetworking/uWebSockets/blob/2d65df86c7988f5d9afa826cfbceda332f06f6a4/misc/main.cpp#L43 https://github.com/uNetworking/uWebSockets/blob/2d65df86c7988f5d9afa826cfbceda332f06f6a4/misc/main.cpp#L48 https://github.com/uNetworking/uWebSockets/blob/2d65df86c7988f5d9afa826cfbceda332f06f6a4/misc/main.cpp#L63 https://github.com/uNetworking/uWebSockets/blob/380b3a7134ee0b52fd0ed02bcef8ddecdf244576/src/App.h#L216

should get updated