colyseus: [Bug]: room.state in client is undefined
Context
No response
Bug description
In the room object returned by Client.create method, state field is undefined.
But, in server, the room.state is no problem.
Reproduction
No response
Steps to reproduce
import { Client } from 'colyseus.js';
import { createBrowserRouter } from 'react-router-dom';
const client = new Client(...);
createBrowserRouter([
{
path: '/room/create',
loader: async (): Promise => {
return {
room: await client
.create(...)
.then(room => {
// this room.state is undefined!!
return room;
}),
};
}
},
Component: WaitingRoomPage,
},
]);
Environment & Versions
Colyseus: v0.15.0 Node.js: v20.14 OS: ubuntu 22.04
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 20 (8 by maintainers)
Commits related to this issue
- fix ESM build to use 'useDefineForClassFields', so property descriptors work properly. colyseus/colyseus#739 — committed to colyseus/schema by endel 2 years ago
- upgrade rollup modules. fixes ESM build. colyseus/colyseus#739 — committed to colyseus/schema by endel 2 years ago
It turns out the ESM build from
@colyseus/schemawas not using the"useDefineForClassFields"config. I’ve just fixed this on the latest@colyseus/schemaversion.The cause was because the Reflection types were compiled as follows:
Whereas they should’ve been like this, so we can inject our own property descriptors in order to detect changes and encode them:
Let me know if you have any other issues! Thanks for reporting again.
That’s on
Room.jsfrom the SDK: https://github.com/colyseus/colyseus.js/blob/1f1736bc6301811d9bc713ddcf6be6b7b1de8534/src/Room.ts#L205-L207For some strange reason the server is not sending the handshake bytes. The very first message the client receive from the server is the
Protocol.JOIN_ROOMcontaining the handshake. This is used to initialize the state, butthis.serializer.handshake()is not being called there.I’d suggest debugging in your server why
handshakeCachemight be empty here. This may have some relation with your@shared/typespackage. Let me know what you find with this! https://github.com/colyseus/colyseus/blob/8d71af565813d36450671db65aa039e535de547d/packages/core/src/serializer/SchemaSerializer.ts#L111Hi @neizzz, please check if this configuration is missing on your server-side: https://github.com/colyseus/colyseus/issues/510#issuecomment-1507828422
Also, the state is received asynchronously once the client connects with the server. You’ll need to attach client-side callbacks to your structures: https://docs.colyseus.io/state/schema-callbacks/
If this doesn’t help you, please share your entire sources as there might be something else going on. Cheers