-
Beta Was this translation helpful? Give feedback.
Replies: 12 comments
-
This is brought up in the user manual; WebSockets are valid from open event to close event. That exception you get, "Invalid access of closed uWS.WebSocket..." is not something you should try/catch - it's a hint that you are using the library wrong. Once you get the close event, you can no longer use the WebSocket whatsoever (it is dead). This means if you have a data structure that holds sockets in a lobby you have to properly remove all references to the dead WebSocket from that data structure so that you don't access it by mistake. There is no way to "check" if a WebSocket is dead or not, you have to properly make use the the open/close events to update whatever data structures you have. Btw, if you're building a lobby you should make use of the pub/sub functions built-in to get that feature for free and with much better performance. |
Beta Was this translation helpful? Give feedback.
-
Yeah, I'm using the close event to already clean the stuff out, that's the part I'm not sure why I have a redundancy of _ws but I'm gonna dig in then. I also put flags to "destroyed" so my interface is broken somewhere lol. Thanks for the hint. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I really have no idea what you are doing but it sounds very wrong. You shouldn't have any setTimeout hacks or anything like that. It's really really simple; you have open and close and you may end or close the connection. The rest is handled for you. |
Beta Was this translation helpful? Give feedback.
-
Ah lol no you don't get it, my first code screenshot is a "game-instance", when an instance is destroyed it is disconnecting sockets. But in case the client isn't sending the disconnect instruction, the server force the disconnection 250ms after. Nothing wrong here, game logic. The close/disconnect "home-made" functions are to prevent a dev doing mistakes with the socket and also being able to know which player instance has been disconnected. Anyway, everything works fine since your first answer and my fix ^^ |
Beta Was this translation helpful? Give feedback.
-
The server can disconnect with a message and code, so your client can differ between a graceful disconnect and a problematic disconnect. That way you don't need to introduce a bunch of JavaScript timeouts |
Beta Was this translation helpful? Give feedback.
-
I tried that, message never end on client side. |
Beta Was this translation helpful? Give feedback.
-
Onclose is called with a CloseEvent holding code and reason: https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent Whatever you pass to ws.end(code, reason) server side ends up in that CloseEvent client side. |
Beta Was this translation helpful? Give feedback.
-
Ok yeah I saw that there are some default codes like 1001 and 1006 but I tried custom stuff maybe I did it wrong. Will try again this could indeed make things much more easier for me :) |
Beta Was this translation helpful? Give feedback.
-
Yes I think you are complicating things more than what you have to |
Beta Was this translation helpful? Give feedback.
-
Ah honestly it's not that complex with what I sent, I just have to be careful when server disconnect someone for now, so it'll make future architectures easier, but now it's already setup it's fine x) |
Beta Was this translation helpful? Give feedback.
-
https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent this.sockets_i_have_to_disconnect = []; this.players.forEach(pl => { Also dunno exactly your logic but to close sockets when game ends doesn't look as strong logic, a better logic is to move them to another room a lobby something so they can start a new game, chat dunno. |
Beta Was this translation helpful? Give feedback.
This is brought up in the user manual; WebSockets are valid from open event to close event. That exception you get, "Invalid access of closed uWS.WebSocket..." is not something you should try/catch - it's a hint that you are using the library wrong. Once you get the close event, you can no longer use the WebSocket whatsoever (it is dead).
This means if you have a data structure that holds sockets in a lobby you have to properly remove all references to the dead WebSocket from that data structure so that you don't access it by mistake.
There is no way to "check" if a WebSocket is dead or not, you have to properly make use the the open/close events to update whatever data structures you have.