RiptideNetworking / Riptide

Lightweight C# networking solution for multiplayer games.
https://riptide.tomweiland.net
MIT License
1.04k stars 141 forks source link

Client instance getting timed out after reconnecting #105

Closed KevinAndok closed 10 months ago

KevinAndok commented 11 months ago

So there is an issue when a client connects and disconnects from the server and then: reconnecting with the same Client instance sometimes does not work; you get timed out after a few seconds. To reproduce this, you can use the ConsoleServer demo and I have rewritten the ConsoleClient to connect and disconnect when enter is pressed. Use this code for the client:

using Riptide.Utils;
using System;
using System.Threading;

namespace Riptide.Demos.ConsoleClient
{
    internal class Program
    {
        private static Client client;
        private static bool isRunning;

        private static void Main()
        {
            client = new Client();
            client.Connected += (s, e) => Console.WriteLine("Press enter to disconnect at any time.");
            client.Disconnected += (s, e) => Console.WriteLine("Press enter to connect at any time.");

            RiptideLogger.Initialize(Console.WriteLine, true);

            new Thread(new ThreadStart(Loop)).Start();

            while (true)
            {
                Console.ReadLine();

                isRunning = !isRunning;

                if (isRunning)
                    client.Connect("127.0.0.1:7777");
                else
                    client.Disconnect();
            }
        }

        private static void Loop()
        {
            while (true)
            {
                if (isRunning)
                {
                    client.Update();
                }

                Thread.Sleep(10);
            }
        }
    }
}