How to get the connection ID of the client that issued a Command

Does anyone know how to get the connection ID of a client that issued an HLAPI Command?

The purpose being that I want to make a request to the server via a Command, and have the server eventually respond to that request with a UNET Message to that client’s connection ID. But I can’t find any examples of capturing the connection ID of a client that runs a Command.

connectionToClient returns a NetworkConnection for the specific NetworkBehaviour component.
https://docs.unity3d.com/ScriptReference/Networking.NetworkIdentity-connectionToClient.html

[Command]
public void CmdTest()
{
   Debug.Log("A Client called a command, client's id is: " + connectionToClient);
}
1 Like

Exactly what I was looking for, thanks!

1 Like

Doesn’t this (connectionToClient) only apply for objects that the client has authority over?

I think in the event that you want to grab the connection of the client that called the Command for an object without authority, you need to manually pass a reference of client’s network id. Not certain though.

Unet generates an error instead of sending the Command if the client doesn’t have authority over the object.

Oh… that’s a shame. Good to know though! In Mirror you can ignore authority and pass the client’s connection through the method declaration, but perhaps that’s yesterday’s news.

1 Like

I’ve an issue with this:

connectionToClient.connectionId in the CMD method is not the same as connectionToServer.connectionId in the method that is calling the CMD method. It seems that the connectionId for a players connection on the server is different than the connectionId on the client? How can I identify, that the connection on the client is the same connection as the one that called the server?

Thank you :slight_smile:

1 Like