Move socket operations to device manager

This commit is contained in:
Christoph Hagen 2023-09-07 14:05:41 +02:00
parent 107b609aea
commit e77efe795c
2 changed files with 14 additions and 12 deletions

View File

@ -15,7 +15,7 @@ final class DeviceManager {
private let remoteKey: Data private let remoteKey: Data
/// Indicate that the socket is fully initialized with an authorized device /// Indicate that the socket is fully initialized with an authorized device
var deviceIsAuthenticated = false private var deviceIsAuthenticated = false
private var isOpeningNewConnection = false private var isOpeningNewConnection = false
@ -62,7 +62,7 @@ final class DeviceManager {
// MARK: API // MARK: API
var deviceStatus: String { private var deviceStatus: String {
deviceIsConnected ? "1" : "0" deviceIsConnected ? "1" : "0"
} }
@ -148,6 +148,18 @@ final class DeviceManager {
func createNewDeviceConnection(_ socket: WebSocket) { func createNewDeviceConnection(_ socket: WebSocket) {
defer { updateDeviceConnectionMetric() } defer { updateDeviceConnectionMetric() }
socket.onBinary { _, data in
self.processDeviceResponse(data)
}
socket.onText { _, text in
self.authenticateDevice(hash: text)
}
_ = socket.onClose.always { _ in
self.didCloseDeviceSocket()
}
isOpeningNewConnection = true isOpeningNewConnection = true
removeDeviceConnection() removeDeviceConnection()
connection = socket connection = socket

View File

@ -76,16 +76,6 @@ func routes(_ app: Application) throws {
- Note: The first message from the device over the connection must be a valid auth token. - Note: The first message from the device over the connection must be a valid auth token.
*/ */
app.webSocket(RouteAPI.socket.path) { req, socket in app.webSocket(RouteAPI.socket.path) { req, socket in
socket.onBinary { _, data in
deviceManager.processDeviceResponse(data)
}
socket.onText { _, text in
deviceManager.authenticateDevice(hash: text)
}
_ = socket.onClose.always { _ in
deviceManager.didCloseDeviceSocket()
}
deviceManager.createNewDeviceConnection(socket) deviceManager.createNewDeviceConnection(socket)
} }
} }