Improve socket connection and logging

This commit is contained in:
Christoph Hagen 2023-12-08 16:28:48 +01:00
parent 1917d1d10e
commit 9feab409ab

View File

@ -140,7 +140,8 @@ final class DeviceManager {
func createNewDeviceConnection(socket: WebSocket, auth: String) async { func createNewDeviceConnection(socket: WebSocket, auth: String) async {
guard let key = Data(fromHexEncodedString: auth), guard let key = Data(fromHexEncodedString: auth),
SHA256.hash(data: key) == self.deviceKey else { SHA256.hash(data: key) == self.deviceKey else {
log("Invalid device key") log("Invalid device key while opening socket")
try? await socket.close()
return return
} }
await removeDeviceConnection() await removeDeviceConnection()
@ -149,6 +150,12 @@ final class DeviceManager {
socket.eventLoop.execute { socket.eventLoop.execute {
socket.pingInterval = .seconds(10) socket.pingInterval = .seconds(10)
socket.onText { socket, text in
print("[WARN] Received text over socket: \(text)")
// Close connection to prevent spamming the log
try? await socket.close()
}
socket.onBinary { [weak self] _, data in socket.onBinary { [weak self] _, data in
self?.processDeviceResponse(data) self?.processDeviceResponse(data)
} }