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

Leaked connections when trying to connect to a high latency server #7

Open
jlcvp opened this issue Mar 24, 2021 · 7 comments
Open

Leaked connections when trying to connect to a high latency server #7

jlcvp opened this issue Mar 24, 2021 · 7 comments

Comments

@jlcvp
Copy link
Contributor

jlcvp commented Mar 24, 2021

This following snippet of code in the project is creating multiple connections when connecting to high latency servers. The while loop calls self.client?.connect again (thus, creating another websocketclient) if the first call takes more than 200 + self._checksDelay millis to connect/fail

if !self.isConnected() {
self.client?.disconnect()
usleep(200_000)
self.client?.connect()
usleep(self._checksDelay)
self.updateLastPoint()
continue
}

The problem with this busy wait approach is that we don't have a way outside this library code to manage those connections and terminate the duplicates if needed.

I think we Should try to use a Lock before the if !self.isConnected() { check to do this verification only after the connection handshake itself. That way we would have only 2 states possible (connection successful or connection failed), hence avoiding the duplicates

@ankitpoplify
Copy link

@jlcvp I'm trying to test this library with one of my projects and I found that it is creating 6 connections at a time. Is this because of this code snippet?

@nerzh
Copy link
Owner

nerzh commented Jun 23, 2022

I think we need to remove the reconnect code. I will remove it and let the ios developer himself be responsible for this

@ankitpoplify
Copy link

@nerzh Which reconnect code?

@nerzh
Copy link
Owner

nerzh commented Jun 23, 2022

disable reconnect


passing the parameter false.
Next, implement the logic that suits you. You will need to monitor the connection and, in case of a break, reconnect manually by calling the connect method.

@ankitpoplify
Copy link

I’m using this for live chat, can you please give some basic idea for how to implement logic to monitor connection?

@nerzh
Copy link
Owner

nerzh commented Jun 23, 2022

you have a bunch of connection lifecycle callbacks. Implement your logic, what to do if the connection is broken or for some reason the ping stops coming, etc. All servers are different, they work differently, so it’s difficult to write a universal reconnection. At the moment you say you have 6 connections, I don't know how you counted that, but if that's the case, then implement the reconnection logic yourself that will do what you want. To do this, handle all situations in which the connection was interrupted

@nerzh
Copy link
Owner

nerzh commented Jun 23, 2022

these callbacks should help you

public func addOnDisconnected(_ handler: @escaping (_ reason: String?) -> Void) {

public func addOnCancelled(_ handler: @escaping () -> Void) {

public func addOnPing(_ handler: @escaping () -> Void) {

public func addOnPong(_ handler: @escaping () -> Void) {

Repository owner deleted a comment from sxnkr Feb 20, 2024
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

3 participants