socketio / socket.io-client-swift

Other
5.22k stars 842 forks source link

Just after `connected` socket status become `disconnected` #1349

Open mrfarukturgut opened 3 years ago

mrfarukturgut commented 3 years ago

Hi all. This is my socket service.


class SocketService {

    private var manager: SocketManager!
    private var socket: SocketIOClient!
    private var status: SocketIOStatus!

    private var currentChat: ChatDTO?

    private var socketURL: URL {
        URL(string: "https://myUrl.com")!
    }

    private var token: String {
        Authorization.bareToken ?? String()
    }

    private let configuration: SocketIOClientConfiguration = [
        .log(true),
        .compress,
        .connectParams(["token": Authorization.bareToken ?? .init()]),
        .version(SocketIOVersion(rawValue: 2)!)
    ]

    init() {
        manager = SocketManager(socketURL: socketURL, config: configuration)
        socket = manager.socket(forNamespace: "/myNamespace")
        status = .disconnected

        socket.on(clientEvent: .statusChange) { (data, emitter) in
            print("--status have changed to ", data)
            guard let status = data[0] as? SocketIOStatus else {
                return
            }
            self.status = status

        }

        self.socket.on("message") { (data, emitter) in

        }

    }

    func write(_ message: String, chatId: String, toUserId: Int) {
        ...
    }

    func start() {
        socket.connect()
    }
}

And a controller class

class Controller: UIViewController {

    private let service = SocketService()

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        service.start()
    }

}

Above is working just fine. However I needed to make some changes to be able to use one socket appwide and decided to create and manage SocketService in another service called ChatService

class ChatService {

    private let socketService = SocketService()

    func start() {
        socketService.start()
    }

}

class AnotherController: UIViewController {

    private let service = ChatService()

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        service.start()
    }

}

With configuration, which I dont change anything in the SocketService, it was first keeping loop connecting/connected/disconnected. There are some similar issues opened and closed right now.

I made some changes, which I dont remember exactly right now, and it now just connecting/connected/disconnected and stops. The first code config, creating SocketService instance in a Controller and specifically starting it in viewWillAppear works just fine.

I was using 16.0.1 and saw in a similar issue that upgrading 15.2.0 to 16.0.0 resolved the problem. It did now work for me.

mrfarukturgut commented 3 years ago

Making SocketService singleton fixed the issue. However I have faced some other issues when I just started to use the library, like it did not work at first when declared the instances in the init. Moving the declarations to the class fixed it. It seems like some certain things breaks the library, or it is build that way i dont know, thus those stuff should be detected and mentioned in the docs to avoid further confusions.