Skip to content

Commit

Permalink
remoteRoomCall: force JSON serialization on local (same-process) calls
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Oct 17, 2022
1 parent ca7c61f commit 151668b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 21 additions & 0 deletions bundles/colyseus/test/MatchMakerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,27 @@ describe("MatchMaker", () => {
assert.strictEqual(true, rooms[2].locked);
});

it("remote room call should always serialize as JSON", async () => {
matchMaker.defineRoomType('remoteroomcall', class _ extends Room {
methodName(arg1, arg2) {
return [arg1, arg2];
}
});

class CustomClass {
attr = 1;
}

const reservedSeat = await matchMaker.joinOrCreate("remoteroomcall");
const result = await matchMaker.remoteRoomCall(reservedSeat.room.roomId, "methodName", [new CustomClass(), new CustomClass()]);

assert.ok(!(result[0] instanceof CustomClass));
assert.ok(!(result[1] instanceof CustomClass));

assert.strictEqual(result[0].attr, 1);
assert.strictEqual(result[1].attr, 1);
});

describe("concurrency", async () => {
it("should create 50 rooms", async () => {
const numConnections = 100;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/MatchMaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export async function remoteRoomCall<R= any>(
} else {
return (!args && typeof (room[method]) !== 'function')
? room[method]
: (await room[method].apply(room, args));
: (await room[method].apply(room, args && args.map((arg) => JSON.parse(JSON.stringify(arg)))));
}
}

Expand Down

3 comments on commit 151668b

@WeroldFrannie
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why force JSON serialization?
I could pass a class type as an arg to remoteRoomCall before, but now I can't :(

@endel
Copy link
Member Author

@endel endel commented on 151668b Mar 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @WeroldFrannie, it was never intended to allow passing complex structures from remoteRoomCall.

This method is meant for communicating across different Node.js processes. When the remote room lives on a different Node.js process every argument is going to be encoded. Forcing JSON serialization is a way to avoid unexpected behavior in both scenarios (same process, or on a different process)

I believe you could come up with your own structure if you'd like to pass complex objects between rooms, although I wouldn't recommend that because that won't work when scaling your system

@WeroldFrannie
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your reply.

I had code design like this
image

Maybe it's too complex that I should modify the design to satisfy the new update. 😭

Please sign in to comment.