Why the func received_match_state is not being called?

I’m making a game that its matches only have 2 players
When the second player joins a match, it should send a message to his opponent so they can both go to the “Accept Match” screen

I’m trying to achieve that by calling
GameManager.socket.send_match_state_async(match_id, op_code, json_message)
method

And hoping to receive the response on _on_received_match_state function, that is also in the GameManager class, but its never triggered!

I already connected this function to the received_match_state event like this: socket.received_match_state.connect(_on_received_match_state)

The match_id is correct, socket is also alive
I’m checking with GameManager.socket.is_connected_to_host(): and its all good but _on_received_match_state is not triggered by my send_match_state_async func!

I’m days stuck at this problem.
Any light for me?

Thanks in advance!

GameManager class (that have socket, client, session logic) :


socket.received_match_state.connect(_on_received_match_state)

func _on_received_match_state(match_state: NakamaRTAPI.MatchData):
	var op_code = match_state.op_code
	var json_message = JSON.parse_string(match_state.data)
	
	if json_message:
		if op_code == 222:
			print("Received message:", json_message["message"])
	else:
		print("Failed to parse JSON message.")

Prematch class, where i call send_match_state_async:

func _on_button_button_up() -> void:
	if not GameManager.socket.is_connected_to_host():
		print("Socket is not connected. Connecting...")
		await GameManager.socket.connect_async(GameManager.session)
	
	var match_id = GameManager.my_match_id
	
	var op_code = 222 
	var message = "if this is shown, it worked!"
	var json_message = JSON.stringify({"message": message})
	
	var result = await GameManager.socket.send_match_state_async(match_id, op_code, json_message)
	if result:
		print("Message sent successfully!")
	else:
		print("Failed to send message.")

Important to say that in this part, it prints “Message sent successfully!”

And i have this in the Output log:

:tv: Media:

Hi @pedromartine!

Could you tell me which version of nakama you are using?
Also could you share with me the code of where the players are joining the match?

Looking at the output I see that that it prints a received match state, isn’t that being triggered by the _on_received_match_state function? Or is this the output of the sender?

Do you have any debug output that shows the other players is receiving the message at all? (assuming the output you showed is the sender).

Just to be sure, you are testing with 2 different clients correct?

Thanks,
Fábio

Hey Fábio, i’m using Nakama 3.22

That’s the code for the join_match_async

func join_match(match_id: String):
	var result = await socket.join_match_async(match_id)
	my_match_id = match_id
	if result.is_exception():
		print("DEU ERRO join_match")
		return
	print(result)

I’m testing both clients in the same machine so i’m not sure if that “Received match state:” is from the user that already is in the match, or from the user that just joined the match

I don’t have any debug output that shows the other players receiving the message :confused:

That’s the _on_received_match_state code:

func _on_received_match_state(match_state: NakamaRTAPI.MatchData):
	print("Received match state:", match_state)
	print("With data:", match_state.data)
	
	var op_code = match_state.op_code
	var json_message = JSON.parse_string(match_state.data)
	
	if json_message:
		print("Parsed JSON message:", json_message)
		if op_code == 222:  # Check if it's the same op code you used to send the message
			print("Received message:", json_message["message"])
	else:
		print("Failed to parse JSON message.")

Is there any discord server that i can show you my screen and the entire context?

Thanks!

Make sure that you connect your listeners both before you connect to the socket, and before you join a match otherwise you’ll miss messages due to the async nature of the socket.

1 Like

how can i use the socket do connect the listeners if i’m not connected to the socket yet?

Are you using godot? If you are in Godot 4, you can run multiple instances from the editor.
It’s in the menu: Debug > Customize Instances…

You can ran multiple instances of the game and got output for all of them. When running, you can also press Stop button beside Play button to stop all instances.

You can also feed each instances with an argument which you can append to the log.

Registering the socket messages callbacks is independent from the socket being connected, if the callbacks aren’t registered before the socket is connected, you may miss messages as there’s no registered handling for them, they’ll be received silently.

I’m already doing all that
Using Godot 4, running multiple instances from the editor, feeding instances with arguments for debugging
Connecting the listeners before connecting to the socket and before joining a match
And more and more and more
Absolutely nothing works

If you guys have 5 minutes to help me, i created a Discord server just for this

If that’s against the rules you can please delete this message or ban me if that’s the punishment, i don’t know

@sesposito

We prefer to share knowledge on the Forum so that it becomes a centralized knowledge center and the information is better structured so that it may help other members of the community.

Have you inspected the server logs and the Nakama console when the clients have joined the match? Are there no errors or potentially useful logs that may indicate some issue?

Yes, no errors
But let it be, i’m gonna rebuild it with socketio

Thanks y’all