Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

NetworkConnection.SendToClient not working for server?

Discussion in 'Scripting' started by light766, Aug 9, 2017.

  1. light766

    light766

    Joined:
    Apr 23, 2009
    Posts:
    250
    Okay its pretty simple, i want to send the server my id and password when i log in, and obviously i do not want to make a rpc where everyone can see it but only the server can. So I did a pretty simple message and I did it to -1 as the id of the client, and i also tried 0, but neither worked with retrieving or sending the message.

    Notes : I start the server with a NetworkingManager, there is no prefabs other than the networking manager in the scene, and connections are taking place but when i print the count of all clients, the number does not increase after a connection has tooken place.

    TempMsg msg = new TempMsg();
    string temp = "hello";
    msg.msgData = System.Text.Encoding.UTF8.GetBytes(temp);

    //print (NetworkClient.allClients.Count);


    SendToClient(0,MsgType.Highest + 5, msg);

    ...

    public void OnTempMsg(NetworkMessage netMsg)
    {
    TempMsg tempMsg = netMsg.ReadMessage<TempMsg>();
    byte[] recievedBytes = tempMsg.msgData;
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    NetworkServer methods only work on the host. Clients are only aware of the host and not any other clients.

    Easiest way to do what you want is to spawn a networked player prefab that is just an empty with a NetworkBehaviour derivative on it. Then send a Command from that script. This will execute on the host's version only.
     
  3. light766

    light766

    Joined:
    Apr 23, 2009
    Posts:
    250
    Server communication is working like a charm now since i use command, so i did what you said and i made a empty prefab with this code and a network identity, but the server cannot talk to the client now, even with the correct connection id.. which is strange :/

    So this is what I did just to get a communication going was this, but for some reason this is always failing, the server is responding with the correct id...

    Weird thing is, when i do start the server, the server retrieves the message, but none of the clients do..

    public void Start(){

    NetworkManager.singleton.client.RegisterHandler(MsgType.Highest + 5, OnTempMessage);
    CmdSendMyInfo("hello");

    }

    ... (code from above)

    [Command]
    public void CmdSendMyInfo(string whatInfo)
    {

    TempMsg msg = new TempMsg();
    string temp = "hello";

    msg.msgData = System.Text.Encoding.UTF8.GetBytes(temp);

    NetworkServer.SendToClient(transform.GetComponent<NetworkIdentity>().connectionToClient.connectionId, MsgType.Highest + 5, msg);

    }
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I've found connectionToClient/Server to be flaky from time to time. I'd try just caching the connection itself in OnConnectedToServer
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539