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.

Unity Multiplayer Working Client Echo Server based on NetworkServer & NetworkClient + Questions

Discussion in 'Multiplayer' started by rob_vld, Jun 25, 2015.

  1. rob_vld

    rob_vld

    Joined:
    Jul 9, 2013
    Posts:
    184
    Questions:
    1.) How do i tell the Send commands on what channel to send(for both Client and Server)?
    2.) On the client code, networkClient.Configure( connectionConfig, 1 ); I am curious why you have to assign a maximum connection here?
    3.) Be it this is working code, is this code valid?



    SERVER
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. using System;
    5. using System.Linq;
    6. using System.Collections;
    7. using System.Collections.Generic;
    8.  
    9. public class MyMessage : MessageBase
    10.     {
    11.     public int messageInt;
    12.     }
    13.  
    14. public class ManagerNetwork : NetworkBehaviour
    15.     {
    16.     public GameObject prefabPlayer;
    17.  
    18.     public const short  CustomHandlerID = 1000;
    19.     public byte         ChannelUnreliable;
    20.     public byte         ChannelReliableSequenced;
    21.  
    22.     private void Start( )
    23.         {
    24.         NetworkServer.RegisterHandler( MsgType.Connect,     OnClientConnect );
    25.         NetworkServer.RegisterHandler( CustomHandlerID,     MessageFromClient );
    26.  
    27.         ConnectionConfig connectionConfig = new UnityEngine.Networking.ConnectionConfig( );
    28.         connectionConfig.Channels.Clear( );
    29.         ChannelReliableSequenced = connectionConfig.AddChannel( QosType.ReliableSequenced );
    30.         ChannelUnreliable        = connectionConfig.AddChannel( QosType.Unreliable );
    31.      
    32.         NetworkServer.Configure( connectionConfig, 5 );
    33.         NetworkServer.Listen( 22001 );
    34.  
    35.         Debug.Log( "NetworkServer.numChannels " + NetworkServer.numChannels );
    36.         }
    37.  
    38.     private void OnClientConnect( NetworkMessage networkMessage )
    39.         {
    40.         Debug.Log( "OnClientConnect" );
    41.      
    42.         var msg = new MyMessage();
    43.         msg.messageInt = 2456986;
    44.         networkMessage.conn.Send( CustomHandlerID, msg );
    45.         }
    46.  
    47.     private void MessageFromClient( NetworkMessage networkMessage )
    48.         {
    49.         var msg = networkMessage.ReadMessage<MyMessage>();
    50.         Debug.Log( ">> "+msg.messageInt );
    51.         }
    52.  
    53.     }
    CLIENT
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7. public class ManagerNetwork : NetworkBehaviour
    8.     {
    9.     private NetworkClient   networkClient;
    10.     private string          serverIP;
    11.     private int             serverPort;
    12.  
    13.     public const short      CustomHandlerID = 1000;
    14.     public byte             ChannelUnreliable;
    15.     public byte             ChannelReliableSequenced;
    16.  
    17.     public class MyMessage : MessageBase
    18.         {
    19.         public int messageInt;
    20.         }
    21.  
    22.     private void Start( )
    23.         {
    24.         serverIP   = "127.0.0.1";
    25.         serverPort = 22001;
    26.  
    27.         ConnectionConfig connectionConfig = new UnityEngine.Networking.ConnectionConfig( );
    28.         connectionConfig.Channels.Clear( );
    29.         ChannelReliableSequenced = connectionConfig.AddChannel( QosType.ReliableSequenced );
    30.         ChannelUnreliable        = connectionConfig.AddChannel( QosType.Unreliable );
    31.      
    32.         networkClient = new NetworkClient();
    33.         networkClient.RegisterHandler( MsgType.Connect, OnServerConnect );
    34.         networkClient.RegisterHandler( CustomHandlerID, MessageFromServer );
    35.         networkClient.Configure( connectionConfig, 1 );
    36.         networkClient.Connect( serverIP, serverPort );
    37.         }
    38.  
    39.     private void OnServerConnect( NetworkMessage networkMessage )
    40.         {
    41.         Debug.Log( "OnServerConnect" );
    42.         }
    43.  
    44.     private void MessageFromServer( NetworkMessage networkMessage )
    45.         {
    46.         var msg = networkMessage.ReadMessage<MyMessage>();
    47.         Debug.Log( ">> "+msg.messageInt );
    48.      
    49.         MyMessage msg2 = new MyMessage( );
    50.         msg2.messageInt = 2;
    51.         networkClient.Send( CustomHandlerID, msg2 );
    52.         }
    53.     }