mdee5
February 26, 2022, 4:40pm
1
I got an exception while JSON parsing the message.data in MatchLoop function.
let msg = {} as MoveMessage;
try {
msg = JSON.parse(nk.binaryToString(message.data));
} catch (error) {
// Client sent bad data.
logger.debug('Bad data received: %v', error);
continue;
}
From the nakama project template on github
logger.debug('Received move message from user: %v', s.marks);
let mark = s.marks[message.sender.userId] ?? null;
if (mark === null || s.mark != mark) {
// It is not this player's turn.
dispatcher.broadcastMessage(OpCode.REJECTED, null, [message.sender]);
continue;
}
let msg = {} as MoveMessage;
try {
msg = JSON.parse(nk.binaryToString(message.data));
} catch (error) {
// Client sent bad data.
dispatcher.broadcastMessage(OpCode.REJECTED, null, [message.sender]);
logger.debug('Bad data received: %v', error);
continue;
}
if (s.board[msg.position]) {
// Client sent a position outside the board, or one that has already been played.
dispatcher.broadcastMessage(OpCode.REJECTED, null, [message.sender]);
continue;
However, if I just use
msg = JSON.parse(message.data.toString());
it works correctly. Is the code on github wrong or am I doing something incorrectly.
ftkg
February 28, 2022, 12:55pm
2
Hi @mdee5
Which error are you getting, and are you using the latest version of Nakama?
I have same issue with xoxo example (GitHub - heroiclabs/xoxo-phaserjs ). This is because javascript SDK sends plain JSON instead of binary I think. And now I trying to understand how to work with nakama-js-protobuf package
mdee5
March 2, 2022, 1:47pm
4
Sorry for late reply.
I am using nakama 3.3.0 with godot.
Here a results of my research:
Transport serialization type (plain json or protobuf) depends on client side. You should use the Protobuf Adapter with Nakama JS SDK nakama-js/packages/nakama-js-protobuf at master · heroiclabs/nakama-js · GitHub for this.
But protobuf serialization works only for nakama build-in data messages. You should serialize your data by yourself (Protobuf and REST API - #2 by novabyte ) because there is no way to extend nakama build-in protobuf schemas (Decode own protocol protobuf messages in JS client - #2 by mofirouz ). Easiest way is msgpack (MessagePack: It's like JSON. but fast and small. ), but there is a huge owerhead because msgpack data contains field names
JS SDK have issues with sending binary data itself also. Discussion and solution here - Nakama js client has problems with decoding data
Typescript server framework have wrong binary array data declarations. Discussion and solution here - [TS] Fix wrong binary array data declaration · Issue #66 · heroiclabs/nakama-common · GitHub
3 and 4 will be fixed soon i hope