Search Unity

I've got a chat system running, but how can I make it more streamlined? (Flowchart included)

Discussion in 'Multiplayer' started by Acegikmo, Jul 3, 2015.

  1. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    Hello!
    I've made a chat system, which has worked well so far. However, I wish I could make this more streamlined.

    Currently, it seems to me that any client to server communication, has to go via the very first "Player" object you spawn with the NetworkManager, is this correct?

    In this case, I wish I could just go directly via the Chat Relay object when sending a message, but I can't seem to run commands on objects that the client doesn't own.

    Any ideas?

    Cheers :)

    ChatRelayCropped.jpg
     
  2. SuperNeon

    SuperNeon

    Joined:
    Mar 16, 2014
    Posts:
    85
    Hi !
    Apparently you are right, in the manual it is written:
    You can create your own network message and send it to the server :)
    You can take a look in this page: http://docs.unity3d.com/Manual/UNetMessages.html
     
    Acegikmo likes this.
  3. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    Thanks!
    Seems like exactly like what I need.

    I do have another question though - in that example in the documentation, it says:
    Code (CSharp):
    1.    public void Init(NetworkClient client)
    2.     {
    3.         m_client = client;
    4.         NetworkServer.RegisterHandler(MyBeginMsg, OnServerReadyToBeginMessage);
    5.     }
    But Init is not called automatically. Where and when do I call this, and where do I get the NetworkClient from?
    I tried getting it in Awake, from NetworkManager.singleton.client, but it appears to be null.
     
  4. SuperNeon

    SuperNeon

    Joined:
    Mar 16, 2014
    Posts:
    85
    You have to extends the NetworkManager class: http://docs.unity3d.com/Manual/UNetManager.html

    Override this function
    Code (CSharp):
    1. public virtual void OnServerReady(NetworkConnection conn)
    2. {
    3.      GameManager.RegisterServerHandlers();
    4. }
    5.  
    6. public class GameManager
    7. {
    8.     [Server]
    9.     public static void RegisterServerHandlers()
    10.     {
    11.         // here we register our network message handlers.
    12.     }
    13. }
     
  5. Necromantic

    Necromantic

    Joined:
    Feb 11, 2013
    Posts:
    116
    You can use all of the Messaging System without using the NetworkManager (which is just a utility class that implements some default behaviour). All you need to do is handle the Client/Server Connections yourself.
     
  6. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    Alright! I'm still not sure where I would get access to the NetworkClient that I need though, if I use the code above
     
  7. SuperNeon

    SuperNeon

    Joined:
    Mar 16, 2014
    Posts:
    85
  8. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    After some help from the kind people at #unity3d-unet, I got a message-based chat up and running instead, which is much cleaner :)

    Here's the code, in case anyone else needs it!

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using UnityEngine.Networking.NetworkSystem;
    4. using System.Collections;
    5.  
    6. public class ChatRelayer : NetworkBehaviour {
    7.  
    8.     const short CHAT_MSG = MsgType.Highest + 1; // Unique message ID
    9.     public Chat chat; // Separate, non-networked script handling the chat window/interface/GUI
    10.     NetworkClient client;
    11.  
    12.     void Start() {
    13.         NetworkManager netManager = GameObject.FindObjectOfType<NetworkManager>();
    14.         client = netManager.client;
    15.         if( client.isConnected )
    16.             client.RegisterHandler( CHAT_MSG, ClientReceiveChatMessage );
    17.         if( isServer )
    18.             NetworkServer.RegisterHandler( CHAT_MSG, ServerReceiveChatMessage );
    19.    
    20.     }
    21.  
    22.     public void SendChatMessage(string msg) {
    23.         StringMessage strMsg = new StringMessage( msg );
    24.         if( isServer ) {
    25.             NetworkServer.SendToAll( CHAT_MSG, strMsg ); // Send to all clients
    26.         } else if(client.isConnected ) {
    27.             client.Send( CHAT_MSG, strMsg ); // Sending message from client to server
    28.         }
    29.     }
    30.  
    31.     public void ServerReceiveChatMessage( NetworkMessage netMsg ) {
    32.         string str = netMsg.ReadMessage<StringMessage>().value;
    33.         if( isServer ) {
    34.             SendChatMessage( str ); // Send the chat message to all clients
    35.         }
    36.     }
    37.  
    38.     public void ClientReceiveChatMessage( NetworkMessage netMsg ) {
    39.         string str = netMsg.ReadMessage<StringMessage>().value;
    40.         if( client.isConnected ) {
    41.             chat.AppendMessage( str ); // Add the message to the client's local chat window
    42.         }
    43.     }
    44.  
    45. }
     
    avivoren, nikkolar, Lelon and 3 others like this.
  9. avivoren

    avivoren

    Joined:
    Sep 21, 2014
    Posts:
    42
    Thank you, helped me alot! :)