heroiclabs / nakama-defold

Defold client for Nakama server.
https://heroiclabs.com
Apache License 2.0
74 stars 12 forks source link

Sending match data does not invoke a response #77

Closed msesen closed 2 weeks ago

msesen commented 2 weeks ago

No response is received when sending match data using the socket.match_data_send() method.

Sample project attached. The project is based on this repo. Sample code added to the example.script at the end of the init() lifecycle method.

nakama-defold-master - match-data-send-no-response.zip

image

linear[bot] commented 2 weeks ago

SDK-822 Sending match data does not invoke a response

britzl commented 2 weeks ago

It's hard to tell what the problem could be. If you get absolutely no reply then I'd start looking at your server logic and the match_loop(). Perhaps you have a crash there? Do you see anything in the server logs?

msesen commented 2 weeks ago

No errors in the server logs. The only log is the data that was sent by the client, which I log inside the match_loop(). I am able to join a match, receive messages from the server, as well as listen for errors on socket.on_error().

I have followed the official guide to setup the server, which is running on a docker container within a ubuntu virtual machine. I can access the Nakama console via the browser, login etc.

I will go through the server setup process again and test it with a basic match_handler module.

msesen commented 2 weeks ago

I have created a fresh server using Docker desktop on Windows and followed the official guide. Added a basic match_handler module, which I have got it from https://heroiclabs.com/docs/nakama/server-framework/lua-runtime/code-samples/

Unfortunately, I am still unable to get a response when sending match data using socket.match_data_send(). The data is received by the server, gets added to the log and that's it.

{"level":"debug","ts":"2024-06-10T18:44:20.577Z","caller":"server/pipeline.go:65","msg":"Received *rtapi.Envelope_MatchDataSend message","uid":"13cf1be7-f8b2-4cd2-8e82-c7ca6db52715","sid":"72ec902f-2759-11ef-8069-99837944f4bc","cid":"1","message":{"MatchDataSend":{"match_id":"f4e1fbdd-bda4-4880-a2e8-d1a8f0783178.nakama_1","op_code":10,"data":"WyJUZXN0IERhdGEiXQ=="}}}

I have the following added as dependencies to the Defold project:

Nakama server runtime version: 3.22.0

I have tried this on 3 different computers with no luck.

msesen commented 2 weeks ago

I've added a before and after MatchDataSend hooks per the documentation and they work fine.

Here are the server logs. The message "Hello World" is successfully received and logged within the match_loop function.

image

Also, if I invoke the error() method within the before hook function, a response is received and the callback is invoked (with the 'error message').

image

britzl commented 2 weeks ago

Really hard to say what's going on. I can take a look if you want? You can share a project where this can be tested with me (bjorn@defold.se).

msesen commented 2 weeks ago

Hi @britzl

I will be greatful if you can have a quick peek and see if you can figure out what's going on!

I have created a blank/default Defold project which automatically adds a main collection. I've added a go and attached a script to it. I've added the sample code within the init() method of the script along with the simple module files I have created for the auth and helper methods to help you quickly run the code.

There is a docker-compose.yml file inside the server/docker/ folder. If you have docker desktop you can start a command prompt/terminal and run docker compose up to start the server (I'm sure you know this :) ). You can access the server log which is inside the server/docker/data/logfile.log.

You can access the Nakama console using the browser at http://127.0.0.1:7351 and the username is nakama, password is testpassword. This info is available in the config.yml file inside trhe data folder. The Lua server runtime modules are also inside the data folder which is registered when you run docker compose up.

image

image

You will see that the final print statement ('The End') isn't executed.

Sample project

defold_nakama_match_send_data.zip

britzl commented 2 weeks ago

Ok, so there's at least one problem with the code you shared. In main.script you use a hardcoded matchid instead of match.matchid:

Screenshot 2024-06-12 at 08 47 04

If I make this change I start getting match data back if I set a match data listener:

socket.on_match_data(function(message)
    pprint(message)
end)

But I'm still not entirely sure why socket.match_data_send() never completes. I still need to look into this.

msesen commented 2 weeks ago

Ok, so there's at least one problem with the code you shared. In main.script you use a hardcoded matchid instead of match.matchid:

Screenshot 2024-06-12 at 08 47 04

If I make this change I start getting match data back if I set a match data listener:

socket.on_match_data(function(message)
    pprint(message)
end)

But I'm still not entirely sure why socket.match_data_send() never completes. I still need to look into this.

Thanks for having a quick look. The matchid isn't hardcoded in the sample project. Please see my previous post.

I am able to receive socket events including the match data. The main issue is that socket.match_data_send() never completes (regardless of whether there is a valid match or not). If I/we can figure out why, then this issue would be solved 🤞.

britzl commented 2 weeks ago

Thanks for having a quick look. The matchid isn't hardcoded in the sample project. Please see my previous post.

I am able to receive socket events including the match data. The main issue is that socket.match_data_send() never completes (regardless of whether there is a valid match or not). If I/we can figure out why, then this issue would be solved

Ok, I think I know why. If you look at the Socket.cs of the .NET client you see that some socket messages set the "Cid" field (the callback id). Example AddMatchmaker:

https://github.com/heroiclabs/nakama-dotnet/blob/master/Nakama/Socket.cs#L203-L223

But not all of the socket messages set a "Cid", for instance SendMatchState:

https://github.com/heroiclabs/nakama-dotnet/blob/master/Nakama/Socket.cs#L613-L627

Messages where the "Cid" is set expect a callback reply from the backend, while messages without a "Cid" do not. In the Defold client all functions expect a callback, including the match data message. The fix will be to exclude certain messages from setting a Cid. I will prepare a PR.

msesen commented 2 weeks ago

Ahh yes you are correct! I can test the changes when you are ready, and hopefully it's a quick fix.

britzl commented 2 weeks ago

Ahh yes you are correct! I can test the changes when you are ready, and hopefully it's a quick fix.

You can try this branch: https://github.com/heroiclabs/nakama-defold/archive/refs/heads/issue-77-no-match-data-response.zip

msesen commented 2 weeks ago

Ahh yes you are correct! I can test the changes when you are ready, and hopefully it's a quick fix.

You can try this branch: https://github.com/heroiclabs/nakama-defold/archive/refs/heads/issue-77-no-match-data-response.zip

The socket.match_data_send() now completes successfully. I have tested from a coroutine and also outside of a coroutine using a callback. In both instances it has completed. The message was received by the server and I was able to access it within the match_loop.

There is still no result returned, it just returns an empty table but I believe this is by design.

I am suprised no one has this flagged up earlier, nevertheless, I am super happy and can continue using Nakama!

Thank you @britzl !

Edit: I've tested this with the latest version of the Defold Editor and the latest version of the Nakama server (3.22.0).

britzl commented 2 weeks ago

Good to hear that it is working! It could be that users are sending match data and don't care if it returns or not