GithubHelp home page GithubHelp logo

Comments (4)

OpNop avatar OpNop commented on May 16, 2024 2

No problem, yes I ended up writing this, feels a bit messy, but works

private void ConnectToServer(bool reconnect = false)
{
    //Dispose of old connection if reconnecting
    if (reconnect)
        client.Dispose();

    _log.AddInfo($"Connecting to {serverAddr}:{serverPort}");
    client = new TcpClient(serverAddr, serverPort, false, null, null);
    client.Connected += ServerConnected;
    client.Disconnected += ServerDisconnected;
    client.DataReceived += MessageReceived;
    try
    {
        client.Connect();
    }
    catch (Exception)
    {
        WriteToChat("Can not connect to server, retrying in 5 seconds");
        if(!reconnect) new Thread(TryReconnect).Start();
    }
}

private void ServerDisconnected(object sender, EventArgs e)
{
    WriteToChat("Server disconnected, Retrying in 5 seconds.");
    new Thread(TryReconnect).Start();
}

private void TryReconnect()
{
    while (!client.IsConnected)
    {
        Thread.Sleep(5000);
        ConnectToServer(true);
    }
}

from supersimpletcp.

jchristn avatar jchristn commented on May 16, 2024

Hi @OpNop try reinstantiating the entire client object inside your TryReconnect method ie client = new TcpClient(... and attaching your callbacks in there. Once itโ€™s disconnected the TcpClient object has to be reinstantiated.

Iโ€™ll be back at my computer tomorrow night and can provide more detail. Sorry Iโ€™m mobile right now (which explains the less than thorough answer!).

from supersimpletcp.

jchristn avatar jchristn commented on May 16, 2024

Hi @OpNop did this help solve the issue for you? Sorry I was out of town. Cheers, Joel

from supersimpletcp.

jchristn avatar jchristn commented on May 16, 2024

Glad it works. I'd love to build reconnect logic into the objects themselves but it isn't as clean as I would like. Perhaps in a future version. Cheers!

from supersimpletcp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.