Sending messages to single client

Hello,

what I’d like to do (for testing and helping me to understand how to communicate in UNET) is the following scenario:

Have multiple clients connecting to a server, telling him their names via a [Command] after they connected. Until here its easy, no problems at all. But since Id like to store all necessary information about the players in an array on the server I have to know how to identify them on the network and how to send each of them a message. So how would I approach it for example when trying to send them a message, simpy telling them “Hello (name)” after they connected? Its important here that this message should not be send to all clients.

I know there are some threads about communication already, but I found most of them unanswered or drifting away from my main question. If anyone can help me with that and also try to explain & comment their approach that would be really awesome :slight_smile:

1 Like

As a server, you have NetworkServer.connections. If i were to try and do it, i would make the clients tell the server their network connection id (from NetworkManager,singleton.client.connectionToClient ), so then the server could check with NetworkServer.connections*.connectionId == desiredConnetctionId.*

So the connectionid on server and client is the same? That would be one of my approaches as well. So noe that the server knows where a message should be send (connectionid), how would he do this? As far as I know any kind of RPC will be send to all clients, right?

http://docs.unity3d.com/Manual/UNetMessages.html

This doesnt require a NetworkIdentity, and network messages is where you can use NetworkServer.SendToClient(id, messageType, message); (oh and you can send byte arrays so have fun with network messages)

Is it possible to provide an exact example for above mentioned task to help me understand how network messages work?

//somewhere on player

[Command]
void CmdPlayerName(string newName)
{
    //player sets name
    playerName = newName;
    tableScript.AddPlayer(newName, connectionToServer);
}
//table script example
public class TableScript : MonoBehaviour
{
    List<PlayerItem> players = new List<PlayerItem>();
    public struct PlayerItem
    {
        public string name;
        public int connectionId;
    }
    public class TableMessage : MessageBase
    {
        public byte[] msgData;
    }
    public void AddPlayer(string playerName, NetworkConnection conn)
    {
        PlayerItem newItem = new PlayerItem();

        newItem.name = playerName;
        newItem.connectionId = conn.connectionId;

        players.Add(newItem);
    }
    public void OnTableMessage(NetworkMessage netMsg)
    {
        TableMessage tableMessage = netMsg.ReadMessage<TableMessage>();
        byte[] recievedBytes = tableMessage.msgData;

        //recieved the bytes, here you can deserialize these bytes into a message
    }
    //to send you reference the TableScript component and use SendToClient("PlayerName", myMessage);
    //myMessage has to be a byte array, so you can serialize your data and send it.
    public void SendToClient(string playerName, byte[] message)
    {
        TableMessage msg = new TableMessage();

        msg.msgData = message;

        int clientId = -1; //-1 is server

        //list through all players, find the player by name and get its connectionId
        for(int i = 0; i < players.Count;i++)
        {
            if(players[i].name == playerName)
            {
                clientId = players[i].connectionId;
            }
        }

        //send a byte array message to a specific clientId
        NetworkServer.SendToClient(clientId, MsgType.Highest + 5, msg);
    }
}

make sure you run this line : NetworkManager.singleton.client.RegisterHandler(MsgType.Highest + 5, OnTableMessage);

as soon as you connect to a server, so you can recieve the message from the server. for example public override void OnClientConnect()

Thank you :slight_smile: I will try to play with this. Can you give me a quick info on the msgType? What is this number for?

Unity already has some built in message types, like MsgType.OnAddPlayer(these are variables with their own numbers), so for your own custom message types, they have to have their own unique number. It can also be a variable, but MsgType.Highest + 5 does it just fine.