Problem
I am using the Lobby guide that you guys created recently, but when I send a message to the backend with an opcode then it won’t get noticed by the matchLoop
and I have no clue why. It only receives the Envelope_MatchDataSend
but I cannot use that in the loop.
Versions
Server version: 3.13.1+2000e4b8
@heroiclabs/nakama-js: 2.4.1
Frontend code
socket.sendMatchState(matchId, 2, "test");
Backend code
export const matchLoop: nkruntime.MatchLoopFunction = (_ctx, logger, _nk, dispatcher, _tick, state, messages) => {
logger.info("----------------- MATCH LOOP -----------------");
logger.debug(JSON.stringify(messages));
messages.forEach((message) => {
logger.debug("------ MESSAGE ------");
logger.debug(JSON.stringify(message));
// If the message is a Ready message, update the player's isReady status and broadcast it to other players
if (message.opCode === 2) {
state.players[message.sender.userId].isReady = true;
dispatcher.broadcastMessage(2, JSON.stringify({ userId: message.sender.userId }));
// Check to see if all players are now ready
const allReady = !Object.keys(state.players).find((userId) => !state.players[userId].isReady);
// If all players are ready, broadcast the game starting event
if (allReady && Object.keys(state.players).length === state.settings.requiredPlayerCount) {
dispatcher.broadcastMessage(MatchOpCode.GAME_START);
}
}
});
return {
state,
};
};
Logs
[backend] {"level":"info","ts":"2022-10-20T11:14:55.813Z","caller":"server/runtime_javascript_logger.go:74","msg":"----------------- MATCH LOOP -----------------","mid":"73fc85cf-3f9d-48e1-af2e-564bbd654ff0"}
// The messages object
[backend] {"level":"debug","ts":"2022-10-20T11:14:55.814Z","caller":"server/runtime_javascript_logger.go:104","msg":"[]","mid":"73fc85cf-3f9d-48e1-af2e-564bbd654ff0"}
// The received opcode as envelope
[backend] {"level":"debug","ts":"2022-10-20T11:14:56.785Z","caller":"server/pipeline.go:65","msg":"Received *rtapi.Envelope_MatchDataSend message","uid":"a1f2c9a8-b1c9-448d-bda2-a56a44aecd5b","sid":"67112cbc-5068-11ed-92ad-006100a0eb06","message":{"MatchDataSend":{"match_id":"73fc85cf-3f9d-48e1-af2e-564bbd654ff0.nakama","op_code":1,"data":"dGVzdA=="}}}
// But sadly still no pickup of the message received, empty messages object
[backend] {"level":"info","ts":"2022-10-
20T11:14:56.813Z","caller":"server/runtime_javascript_logger.go:74","msg":"----------------- MATCH LOOP -----------------","mid":"73fc85cf-3f9d-48e1-af2e-564bbd654ff0"}
[backend] {"level":"debug","ts":"2022-10-20T11:14:56.814Z","caller":"server/runtime_javascript_logger.go:104","msg":"[]","mid":"73fc85cf-3f9d-48e1-af2e-564bbd654ff0"}
So it only receives the envelope but nothing gets picked up in the matchLoop
so I can intercept it.
What am I doing wrong here?