From 9feab409ab648c1b754be1f5f291a04e89457286 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Fri, 8 Dec 2023 16:28:48 +0100 Subject: [PATCH] Improve socket connection and logging --- Sources/App/DeviceManager.swift | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Sources/App/DeviceManager.swift b/Sources/App/DeviceManager.swift index 8ee45a5..98723a9 100644 --- a/Sources/App/DeviceManager.swift +++ b/Sources/App/DeviceManager.swift @@ -140,7 +140,8 @@ final class DeviceManager { func createNewDeviceConnection(socket: WebSocket, auth: String) async { guard let key = Data(fromHexEncodedString: auth), SHA256.hash(data: key) == self.deviceKey else { - log("Invalid device key") + log("Invalid device key while opening socket") + try? await socket.close() return } await removeDeviceConnection() @@ -149,6 +150,12 @@ final class DeviceManager { socket.eventLoop.execute { 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 self?.processDeviceResponse(data) }