Compare commits
No commits in common. "2b96e49d6febdceb231b48e64c13b3ac40d57c32" and "7c1d46ab776c6dab8cf37cfa5c932c179063d28f" have entirely different histories.
2b96e49d6f
...
7c1d46ab77
@ -29,12 +29,9 @@ final class SocketService {
|
|||||||
connectionStateSubject.value
|
connectionStateSubject.value
|
||||||
}
|
}
|
||||||
|
|
||||||
private let heartbeatInterval: TimeInterval = 10
|
|
||||||
|
|
||||||
#if canImport(SocketIO)
|
#if canImport(SocketIO)
|
||||||
private var manager: SocketManager?
|
private var manager: SocketManager?
|
||||||
private var socket: SocketIOClient?
|
private var socket: SocketIOClient?
|
||||||
private var heartbeatTimer: DispatchSourceTimer?
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
private init() {}
|
private init() {}
|
||||||
@ -105,7 +102,6 @@ final class SocketService {
|
|||||||
setupSocket(with: token)
|
setupSocket(with: token)
|
||||||
updateConnectionState(.connecting)
|
updateConnectionState(.connecting)
|
||||||
socket?.connect(withPayload: currentAuthPayload)
|
socket?.connect(withPayload: currentAuthPayload)
|
||||||
startHeartbeat()
|
|
||||||
#else
|
#else
|
||||||
updateConnectionState(.disconnected)
|
updateConnectionState(.disconnected)
|
||||||
if AppConfig.DEBUG {
|
if AppConfig.DEBUG {
|
||||||
@ -153,27 +149,6 @@ final class SocketService {
|
|||||||
self.updateConnectionState(.connected)
|
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
|
socket.on(clientEvent: .disconnect) { data, _ in
|
||||||
if AppConfig.DEBUG { print("[SocketService] Disconnected: \(data)") }
|
if AppConfig.DEBUG { print("[SocketService] Disconnected: \(data)") }
|
||||||
self.updateConnectionState(.disconnected)
|
self.updateConnectionState(.disconnected)
|
||||||
@ -189,47 +164,12 @@ final class SocketService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func disconnectInternal() {
|
private func disconnectInternal() {
|
||||||
stopHeartbeat()
|
|
||||||
socket?.disconnect()
|
socket?.disconnect()
|
||||||
manager?.disconnect()
|
manager?.disconnect()
|
||||||
socket = nil
|
socket = nil
|
||||||
manager = nil
|
manager = nil
|
||||||
updateConnectionState(.disconnected)
|
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
|
#else
|
||||||
private func disconnectInternal() { }
|
private func disconnectInternal() { }
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user