question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with Connect() immediately after Disconnect()

See original GitHub issue

I am facing an issue when I’m Disconnect()ing from a server and try to reconnect immediately.

The Server I’m connecting to sends a heartbeat every 5 seconds. If no heartbeat is received for more than 5 seconds (plus some grace period), I want to renew the connection by doing

client.Disconnect(); // because IsConnected is still true
client.Connect();

which gives me the following output

[SimpleTcp.Client] disconnecting from 127.0.0.1:8000
[SimpleTcp.Client] initializing client
[SimpleTcp.Client] connecting to 127.0.0.1:8000
Event Connected
[SimpleTcp.Client] data receiver canceled, peer disconnected
Event Disconnected, e.Reason: Normal
[SimpleTcp.Client] dispose complete
[SimpleTcp.Client] data receiver canceled, peer disconnected
Event Disconnected, e.Reason: Normal
[SimpleTcp.Client] dispose complete

This seems like a timing/synchronization problem.

I can work around it by putting a Thread.Sleep(500); after the client.Disconnect();, but that’s probably not the most reliable solution.

Is there a better way?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jchristncommented, Feb 18, 2022
1reaction
powerzone3000commented, Feb 8, 2022

Thank you for your response @jchristn. Unfortunately, the behavior is still the same for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TCP/IP: connecting immediately after disconnecting
I'm working on a client and server protocols over TCP/IP. It is completely asynchronous – that is it uses TcpClient 's methods ...
Read more >
on('disconnect') handler called twice after `disconnect()` if ...
It looks like socket.connect() is causing the second disconnect handler to fire. Perhaps this is because if you call connect() immediately ...
Read more >
Remote Desktop client disconnects and can't reconnect to ...
Troubleshooting an issue in which remote desktop client disconnects and can't reconnect to the same session.
Read more >
Is there a way to reconnect a function after disconnecting it?
I'm trying to create a global cooldown where after I cast a magic ... Unequipped:Connect(function() activated:Disconnect() end) end act().
Read more >
My scripts only work once. Help me using Disconnect()?
I have a lot of scripts in my game that use :connect(function()). After moving through my game, eventually these just stopped working. I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found