Duplicate messages using EventSocket

I am writing a custom iOS app to use as a control center for my home. The strange thing I am seeing is every event appears to be sent sixteen times when I print the events to the console. I added a simple counter and this is what I see in the console:

Switch: off - message: 16
Switch: off - message: 17
Switch: off - message: 18
Switch: off - message: 19
Switch: off - message: 20
Switch: off - message: 21
Switch: off - message: 22
Switch: off - message: 23
Switch: off - message: 24
Switch: off - message: 25
Switch: off - message: 26
Switch: off - message: 27
Switch: off - message: 28
Switch: off - message: 29
Switch: off - message: 30
Switch: off - message: 31
Switch: on - message: 80
Switch: on - message: 81
Switch: on - message: 82
Switch: on - message: 83
Switch: on - message: 84
Switch: on - message: 85
Switch: on - message: 86
Switch: on - message: 87
Switch: on - message: 88
Switch: on - message: 89
Switch: on - message: 90
Switch: on - message: 91
Switch: on - message: 92
Switch: on - message: 93
Switch: on - message: 94
Switch: on - message: 95

I turned the device off and on once from the Habitat device page. You can see all the events I logged. Is this an expected behavior? Otherwise everything is working well.

I am running version. 2.3.6.146 on a C7 hub.

I'm new to Swift but nothing is jumping out at me in the code.

    private func setupEventSocket() {
        let hubitatURL = URL(string: "ws://192.168.107.7/eventsocket")!
        var request = URLRequest(url: hubitatURL)
        request.addValue("Bearer 42...ed", forHTTPHeaderField: "Authorization")

        webSocketTask = URLSession.shared.webSocketTask(with: request)
        receive()
        webSocketTask?.resume()
    }

    private func receive() {
        counter1 += 1
       // print("receive(): \(counter1)")
        webSocketTask?.receive { [weak self] result in
            switch result {
            case .success(let message):
                switch message {
                case .data(let data):
                    //self?.handleHubitatEvent(data)
                    _ = data // just to quiet warnings
                case .string(let text):
                    self?.handleTextMessage(text)
                @unknown default:
                    break
                }
                self?.receive()
            case .failure(let error):
                print("WebSocket receive error: \(error)")
            }
        }
    }

I know Swift is out of scope. I'm just adding some background and hopefully somebody has an explanation for the multiple received messages.

Thanks

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.