Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How do I run two or more clients on one computer and connect to the server?

Discussion in 'Game Server Hosting' started by youngwoocho02, Oct 30, 2023.

  1. youngwoocho02

    youngwoocho02

    Joined:
    Feb 8, 2019
    Posts:
    15
    When I try to join server with build or parrelsync As soon as the connection is established, the connection is disconnected. And this is left in the server log


    and this is client connect code I don't know what to do


    Code (CSharp):
    1. private static string PlayerID
    2.     {
    3.         get
    4.         {
    5.             return AuthenticationService.Instance.PlayerId;
    6.         }
    7.     }
    8.  
    9.     private static string _ticketId;
    10.     private static MultiplayAssignment _multiplayAssignment;
    11.  
    12.     public static async UniTask StartClient()
    13.     {
    14.         Debug.Log(nameof(StartClient));
    15.  
    16.         await InitializeAndSignIn();
    17.  
    18.         await CreateTicket();
    19.     }
    20.  
    21.     private static async UniTask InitializeAndSignIn()
    22.     {
    23.         Debug.Log(nameof(InitializeAndSignIn));
    24.  
    25.         var serverProfileName = $"User{Tool.GetRandomString(10)}";
    26.  
    27.         var initOptions = new InitializationOptions();
    28.         initOptions.SetProfile(serverProfileName);
    29.         await UnityServices.InitializeAsync(initOptions);
    30.  
    31.  
    32.         await AuthenticationService.Instance.SignUpWithUsernamePasswordAsync($"{serverProfileName}", "Password1!");
    33.  
    34.         Debug.Log($"Signed in as {serverProfileName}");
    35.     }
    36.  
    37.     private static async UniTask CreateTicket()
    38.     {
    39.         Debug.Log(nameof(CreateTicket));
    40.  
    41.         var options = new CreateTicketOptions("NetowrkTestQueue01");
    42.         // var options = new CreateTicketOptions();
    43.  
    44.         var players = new List<Player>
    45.         {
    46.             new Player(
    47.                 PlayerID,
    48.                 new MatchData
    49.                 {
    50.                     EnterCode = "Enter"
    51.                 }
    52.             )
    53.         };
    54.  
    55.         var ticketResponse = await MatchmakerService.Instance.CreateTicketAsync(players, options);
    56.         _ticketId = ticketResponse.Id;
    57.  
    58.         await PollTicketStatus();
    59.     }
    60.  
    61.     private static async UniTask PollTicketStatus()
    62.     {
    63.         Debug.Log(nameof(PollTicketStatus));
    64.  
    65.         _multiplayAssignment = null;
    66.         var gotAssignment = false;
    67.  
    68.         do
    69.         {
    70.             await Task.Delay(1000);
    71.             var ticketStatus = await MatchmakerService.Instance.GetTicketAsync(_ticketId);
    72.             if (ticketStatus == null) { continue; }
    73.             if (ticketStatus.Type == typeof(MultiplayAssignment))
    74.             {
    75.                 _multiplayAssignment = ticketStatus.Value as MultiplayAssignment;
    76.             }
    77.  
    78.             Debug.Log($"Ticket Status: {_multiplayAssignment.Status}, Message: {_multiplayAssignment.Message}");
    79.  
    80.             switch (_multiplayAssignment.Status)
    81.             {
    82.                 case MultiplayAssignment.StatusOptions.Found:
    83.                     gotAssignment = true;
    84.                     StartClientConnection(_multiplayAssignment);
    85.                     break;
    86.                 case MultiplayAssignment.StatusOptions.InProgress:
    87.                     break;
    88.                 case MultiplayAssignment.StatusOptions.Failed:
    89.                     gotAssignment = true;
    90.                     Debug.Log("Failed to get assignment");
    91.                     break;
    92.                 case MultiplayAssignment.StatusOptions.Timeout:
    93.                     gotAssignment = true;
    94.                     Debug.Log("Timed out");
    95.                     break;
    96.                 default:
    97.                     throw new InvalidOperationException();
    98.             }
    99.         }
    100.         while (!gotAssignment);
    101.     }
    102.  
    103.     private static void StartClientConnection(MultiplayAssignment assignment)
    104.     {
    105.         Debug.Log(nameof(StartClientConnection));
    106.  
    107.         Debug.Log($"Ticket assigned: {assignment.Ip}:{assignment.Port}");
    108.  
    109.         var networkManager = NetworkManager.Instances.ElementAt(0);
    110.  
    111.         networkManager.ClientManager.StartConnection(assignment.Ip, (ushort)assignment.Port);
    112.     }