Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server disconnects client after ~3 seconds. #101

Open
Chroma72 opened this issue Feb 12, 2021 · 2 comments
Open

Server disconnects client after ~3 seconds. #101

Chroma72 opened this issue Feb 12, 2021 · 2 comments

Comments

@Chroma72
Copy link

Chroma72 commented Feb 12, 2021

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);
            }
        }
    }
}```
@J4z3
Copy link

J4z3 commented Feb 13, 2021

Hi,

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

@Chroma72
Copy link
Author

Ok thanks. Great feature BTW!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants