Improve socket handling
This commit is contained in:
parent
562c6fb9c1
commit
5c4a6597d2
@ -15,10 +15,12 @@ final class DeviceManager {
|
||||
|
||||
/// Indicate that the socket is fully initialized with an authorized device
|
||||
var deviceIsAuthenticated = false
|
||||
|
||||
private var isOpeningNewConnection = false
|
||||
|
||||
/// Indicator for device availability
|
||||
var deviceIsConnected: Bool {
|
||||
!(connection?.isClosed ?? true) && deviceIsAuthenticated
|
||||
deviceIsAuthenticated && !(connection?.isClosed ?? true)
|
||||
}
|
||||
|
||||
/// A promise to finish the request once the device responds or times out
|
||||
@ -75,8 +77,12 @@ final class DeviceManager {
|
||||
}
|
||||
|
||||
func didCloseDeviceSocket() {
|
||||
guard !isOpeningNewConnection else {
|
||||
return
|
||||
}
|
||||
deviceIsAuthenticated = false
|
||||
guard connection != nil else {
|
||||
print("Socket closed, but no connection anyway")
|
||||
return
|
||||
}
|
||||
connection = nil
|
||||
@ -84,14 +90,20 @@ final class DeviceManager {
|
||||
}
|
||||
|
||||
func removeDeviceConnection() {
|
||||
_ = connection?.close()
|
||||
deviceIsAuthenticated = false
|
||||
guard let socket = connection else {
|
||||
return
|
||||
}
|
||||
try? socket.close().wait()
|
||||
connection = nil
|
||||
print("Removed device connection")
|
||||
}
|
||||
|
||||
func createNewDeviceConnection(_ socket: WebSocket) {
|
||||
isOpeningNewConnection = true
|
||||
removeDeviceConnection()
|
||||
connection = socket
|
||||
deviceIsAuthenticated = false
|
||||
print("Socket connected")
|
||||
isOpeningNewConnection = false
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user