Leaked connections when trying to connect to a high latency server
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
https://github.com/nerzh/Action-Cable-Swift/blob/ebb9986f7773db6e9dc035eb6616e6dbcec4bad7/Sources/ActionCableSwift/Helpers.swift#L155-L162
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
@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?
I think we need to remove the reconnect code. I will remove it and let the ios developer himself be responsible for this
@nerzh Which reconnect code?
disable reconnect https://github.com/nerzh/Action-Cable-Swift/blob/ebb9986f7773db6e9dc035eb6616e6dbcec4bad7/Sources/ActionCableSwift/ActionCableSwift.swift#L35 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. https://github.com/nerzh/Action-Cable-Swift/blob/ebb9986f7773db6e9dc035eb6616e6dbcec4bad7/Sources/ActionCableSwift/ActionCableSwift.swift#L72
I’m using this for live chat, can you please give some basic idea for how to implement logic to monitor connection?
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
these callbacks should help you
https://github.com/nerzh/Action-Cable-Swift/blob/ebb9986f7773db6e9dc035eb6616e6dbcec4bad7/Sources/ActionCableSwift/ActionCableSwift.swift#L44
https://github.com/nerzh/Action-Cable-Swift/blob/ebb9986f7773db6e9dc035eb6616e6dbcec4bad7/Sources/ActionCableSwift/ActionCableSwift.swift#L48
https://github.com/nerzh/Action-Cable-Swift/blob/ebb9986f7773db6e9dc035eb6616e6dbcec4bad7/Sources/ActionCableSwift/ActionCableSwift.swift#L60
https://github.com/nerzh/Action-Cable-Swift/blob/ebb9986f7773db6e9dc035eb6616e6dbcec4bad7/Sources/ActionCableSwift/ActionCableSwift.swift#L64