Telepathy icon indicating copy to clipboard operation
Telepathy copied to clipboard

Server disconnects client after ~3 seconds.

Open ghost opened this issue 4 years ago • 2 comments

The title says it all. Here is the server and client code.

Server code:

using System;
using System.Text;
using Telepathy;

namespace TelepathyTestServer {
    class Program {
        static void Main(string[] args) {
            const int MaxMessageSize = 8 * 1024;

            Encoding utf8 = Encoding.UTF8;

            Server server = new Server(MaxMessageSize);

            server.OnConnected = (connectionId) => {
                Console.WriteLine("Client [{0}] connected.", connectionId);
                byte[] msg = utf8.GetBytes("Hello from server.");
                server.Send(connectionId, new ArraySegment<byte>(msg));
            };

            server.OnData = (connectionId, data) => {
                Console.WriteLine(connectionId + " Data: " + utf8.GetString(data.Array, 0, data.Count));
            };

            server.OnDisconnected = (connectionId) => Console.WriteLine("Client [{0}] disconnected.", connectionId);

            server.Start(5200);

            while (true) {

                server.Tick(100);

                System.Threading.Thread.Sleep(1);
            }
        }
    }
}

Client code:

using System;
using System.Text;
using Telepathy;

namespace ClientTest {
    class Program {
        static void Main(string[] args) {
            const int MaxMessageSize = 8 * 1024;

            Encoding utf8 = Encoding.UTF8;

            Client client = new Client(MaxMessageSize);

            client.OnConnected = () => {
                Console.WriteLine("Connected to server.");
                byte[] msg = utf8.GetBytes("Hello from client.");
                client.Send(new ArraySegment<byte>(msg));
            };

            client.OnData = (data) => Console.WriteLine(utf8.GetString(data.Array, 0, data.Count));

            client.OnDisconnected = () => Console.WriteLine("Disconnected from server.");

            client.Connect("127.0.0.1", 5200);
            System.Threading.Thread.Sleep(100);

            while (true) {

                client.Tick(100);

                System.Threading.Thread.Sleep(1);
            }
        }
    }
}```

ghost avatar Feb 12 '21 15:02 ghost

Hi,

This is due to ReceiveTimeout in Common.cs -> set it to 0 or use a periodic heartbeat system.

J4z3 avatar Feb 13 '21 14:02 J4z3

Ok thanks. Great feature BTW!

Chroma72 avatar Feb 14 '21 20:02 Chroma72