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

LOG SocketManager: Tried connecting an already active socket #1466

Open
muneebrehman758 opened this issue Nov 20, 2023 · 0 comments
Open

LOG SocketManager: Tried connecting an already active socket #1466

muneebrehman758 opened this issue Nov 20, 2023 · 0 comments

Comments

@muneebrehman758
Copy link

It connects well first time. When I disconnect and reconnects, it's works fine too. But the problem is once connected, I turn off wifi and wait for it to get disconnected. I turn on the wifi and it never connects back again.

The error it displays is:
LOG SocketManager: Tried connecting socket when engine isn't open. Connecting
LOG SocketManager: Adding engine
LOG SocketEngine: Starting engine. Server: https://staging.test.com/
LOG SocketEngine: Handshaking
LOG SocketManager: Trying to reconnect
LOG SocketManager: Tried connecting an already active socket
LOG SocketManager: Scheduling reconnect in 30.0s

Please note that: "https://staging.test.com/" is not valid. But the actual server that I am using is a valid one.

My socket io helper class is as follows:

class SocketIOHelper {

static let shared = SocketIOHelper()
var manager: SocketManager?
var socket: SocketIOClient?
var timer: Timer?

func initiateConnection() {
    if manager == nil || socket == nil {
        print("😃 Initiate Connection 😃 MSID")
        self.timer?.invalidate()
        self.startTimer()
        self.manager = SocketManager(socketURL: URL(string: WebService.BASE_URL)!, config: [.log(true), .compress, .version(.three), .forceWebsockets(true), .forceNew(true)])
        self.socket = manager?.defaultSocket
        self.addListeners()
        self.socket?.connect()
    } else {
        print("🥵 Destroying Connection 🥵 MSID")
        self.socket?.disconnect()
        self.socket?.removeAllHandlers()
        self.manager?.removeSocket(self.socket!)
        self.manager = nil
        self.socket = nil
        self.initiateConnection()
    }
}

func addListeners() {
    guard let socket = socket else {
        print("🤕 Nil Socket Returned 🤕 MSID")
        return
    }
    
    socket.on(clientEvent: .connect) {data, ack in
        print("✅ Connected ✅ MSID")
    }
    socket.on(clientEvent: .disconnect) {data, ack in
        print("❌ Disconnected ❌ MSID")
    }
    
    socket.on(clientEvent: .error) {data, ack in
        print("⭕ Error ⭕ MSID")
    }
    
    socket.on(clientEvent: .ping) {data, ack in
        print(" 🟡 Ping 🟡 MSID")
    }
    
    socket.on(clientEvent: .pong) {data, ack in
        print("🟠 Pong 🟠 MSID")
    }
    
    socket.on(clientEvent: .websocketUpgrade) {data, ack in
        print("🟣 WebsocketUpgrade 🟣 MSID")
    }
    
    socket.on(clientEvent: .reconnect) {data, ack in
        print("🔶 Reconnect 🔶 MSID")
    }
    
    socket.on(clientEvent: .reconnectAttempt) {data, ack in
        print("🔷 ReconnectAttempt 🔷 MSID")
    }
    socket.on(clientEvent: .statusChange) {data, ack in
        print("⬜ StatusChange to \(self.socket!.status) ⬜ MSID")
    }
}

func startTimer() {
    timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(emitAlive), userInfo: nil, repeats: true)
}

@objc func emitAlive() {
    if self.isSocketConnected() {
        print("🍎 Emit Calling 🍎 MSID")
        let socketData = ["alive_service": "serviceAlive"] as [String : String]
        socket?.emit("wakeup", socketData)
    }else{
        initiateConnection()
    }
}

func isSocketConnected() -> Bool {
    return self.socket?.status == .connected ? true : false
}

func disconnectSocket() {
    guard let socket = self.socket else { return }
    if socket.status == .connected {
        socket.disconnect()
    }
}

}

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

1 participant