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

Most upvoted comments

It turns out the ESM build from @colyseus/schema was not using the "useDefineForClassFields" config. I’ve just fixed this on the latest @colyseus/schema version.

The cause was because the Reflection types were compiled as follows:

class ReflectionField extends Schema {
    name;
    type;
    referencedType;
}

Whereas they should’ve been like this, so we can inject our own property descriptors in order to detect changes and encode them:

class ReflectionField extends Schema {
}

Let me know if you have any other issues! Thanks for reporting again.

For 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_ROOM containing the handshake. This is used to initialize the state, but this.serializer.handshake() is not being called there.

image image

I’d suggest debugging in your server why handshakeCache might be empty here. This may have some relation with your @shared/types package. Let me know what you find with this! https://github.com/colyseus/colyseus/blob/8d71af565813d36450671db65aa039e535de547d/packages/core/src/serializer/SchemaSerializer.ts#L111

Hi @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