colyseus / colyseus.js

⚔ Colyseus Multiplayer SDK for JavaScript/TypeScript
https://docs.colyseus.io/getting-started/javascript-client/
MIT License
407 stars 105 forks source link

On joinOrCreate, room.state is empty #130

Closed perspectivezoom closed 1 year ago

perspectivezoom commented 1 year ago

Is this intended? Or am I missing some trigger or doing something out of order to cause room.state to not be populated?

I've been trying to reproduce https://github.com/ourcade/tic-tac-toe-multiplayer-starter, and I've also looked at https://github.com/endel/colyseus-tic-tac-toe, and both of them seem to assume that room.state is populated from initial join.

If it is intended to be initially empty, how do I trigger the initial population of the room.state?

Description Code console.log
state seems to be populated correctly at onJoin time on the server Screen Shot 2023-09-04 at 6 04 38 PM Screen Shot 2023-09-04 at 6 04 57 PM
On the client side, client.joinOrCreate shows empty values for room.state Screen Shot 2023-09-04 at 6 02 20 PM Screen Shot 2023-09-04 at 6 01 49 PM
Websocket event. Is there a way I can decode the payload to see if the server is sending the right thing or not? N/A Screen Shot 2023-09-04 at 6 03 20 PM
GameState Schema for reference Screen Shot 2023-09-04 at 6 04 17 PM N/A
endel commented 1 year ago

Hi @perspectivezoom, the room.state on the client-side is not immediately populated after connecting, but after a few milliseconds, it does get synchronized.

Both your .listen("activePlayer") and .room.onStateChange(()=>{}) should be triggering after joining the room. If they're not triggering you may be hitting a known server config issue that causes the state not to sync, documented here. If your project was created via npm create colyseus-app@latest that config is already set.

"Websocket event. Is there a way I can decode the payload to see if the server is sending the right thing or not?"

You can use the playground tool to inspect every message https://docs.colyseus.io/tools/playground/


Let me know if that helps, otherwise please share your project so I can take a look. Cheers!

perspectivezoom commented 1 year ago

If they're not triggering you may be hitting a known server config issue that causes the state not to sync, https://github.com/colyseus/colyseus/issues/510#issuecomment-1507828422.

This was totally it. Setting "useDefineForClassFields": false, everything magically works.

the room.state on the client-side is not immediately populated after connecting, but after a few milliseconds, it does get synchronized

According to console.log, I don't ever see the blank state. As far as my application code is concerned, it works immediately.

Screen Shot 2023-09-04 at 7 22 54 PM

You can use the playground tool to inspect every message https://docs.colyseus.io/tools/playground/

It doesn't seem to work for me. I can see the room, but no active connections. But as no longer have an immediate use for inspecting websocket events and this is a problem only tangentially related, I'm closing this issue out. Thanks so much @endel for your help. I've spent pretty much the entire day trying to figure this out.

Screen Shot 2023-09-04 at 7 30 55 PM