Compare commits
No commits in common. "2b96e49d6febdceb231b48e64c13b3ac40d57c32" and "7c1d46ab776c6dab8cf37cfa5c932c179063d28f" have entirely different histories.
2b96e49d6f
...
7c1d46ab77
@ -29,12 +29,9 @@ final class SocketService {
|
||||
connectionStateSubject.value
|
||||
}
|
||||
|
||||
private let heartbeatInterval: TimeInterval = 10
|
||||
|
||||
#if canImport(SocketIO)
|
||||
private var manager: SocketManager?
|
||||
private var socket: SocketIOClient?
|
||||
private var heartbeatTimer: DispatchSourceTimer?
|
||||
#endif
|
||||
|
||||
private init() {}
|
||||
@ -105,7 +102,6 @@ final class SocketService {
|
||||
setupSocket(with: token)
|
||||
updateConnectionState(.connecting)
|
||||
socket?.connect(withPayload: currentAuthPayload)
|
||||
startHeartbeat()
|
||||
#else
|
||||
updateConnectionState(.disconnected)
|
||||
if AppConfig.DEBUG {
|
||||
@ -153,27 +149,6 @@ final class SocketService {
|
||||
self.updateConnectionState(.connected)
|
||||
}
|
||||
|
||||
socket.on(clientEvent: .statusChange) { data, _ in
|
||||
guard let rawStatus = data.first as? SocketIOStatus else { return }
|
||||
if rawStatus == .connected {
|
||||
self.updateConnectionState(.connected)
|
||||
} else if rawStatus == .connecting {
|
||||
self.updateConnectionState(.connecting)
|
||||
} else {
|
||||
self.updateConnectionState(.disconnected)
|
||||
}
|
||||
}
|
||||
|
||||
socket.on(clientEvent: .reconnect) { _, _ in
|
||||
if AppConfig.DEBUG { print("[SocketService] Reconnecting") }
|
||||
self.updateConnectionState(.connecting)
|
||||
}
|
||||
|
||||
socket.on(clientEvent: .reconnectAttempt) { data, _ in
|
||||
if AppConfig.DEBUG { print("[SocketService] Reconnect attempt: \(data)") }
|
||||
self.updateConnectionState(.connecting)
|
||||
}
|
||||
|
||||
socket.on(clientEvent: .disconnect) { data, _ in
|
||||
if AppConfig.DEBUG { print("[SocketService] Disconnected: \(data)") }
|
||||
self.updateConnectionState(.disconnected)
|
||||
@ -189,47 +164,12 @@ final class SocketService {
|
||||
}
|
||||
|
||||
private func disconnectInternal() {
|
||||
stopHeartbeat()
|
||||
socket?.disconnect()
|
||||
manager?.disconnect()
|
||||
socket = nil
|
||||
manager = nil
|
||||
updateConnectionState(.disconnected)
|
||||
}
|
||||
|
||||
private func startHeartbeat() {
|
||||
guard heartbeatInterval > 0 else { return }
|
||||
stopHeartbeat()
|
||||
let timer = DispatchSource.makeTimerSource(queue: syncQueue)
|
||||
timer.schedule(deadline: .now() + heartbeatInterval, repeating: heartbeatInterval)
|
||||
timer.setEventHandler { [weak self] in
|
||||
guard let self else { return }
|
||||
|
||||
guard let socket = self.socket else {
|
||||
self.updateConnectionState(.disconnected)
|
||||
return
|
||||
}
|
||||
|
||||
switch socket.status {
|
||||
case .connected:
|
||||
self.updateConnectionState(.connected)
|
||||
case .connecting:
|
||||
self.updateConnectionState(.connecting)
|
||||
case .disconnected, .notConnected:
|
||||
self.updateConnectionState(.connecting)
|
||||
socket.connect(withPayload: self.currentAuthPayload)
|
||||
@unknown default:
|
||||
self.updateConnectionState(.connecting)
|
||||
}
|
||||
}
|
||||
heartbeatTimer = timer
|
||||
timer.resume()
|
||||
}
|
||||
|
||||
private func stopHeartbeat() {
|
||||
heartbeatTimer?.cancel()
|
||||
heartbeatTimer = nil
|
||||
}
|
||||
#else
|
||||
private func disconnectInternal() { }
|
||||
#endif
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user