SwiftWebSocket icon indicating copy to clipboard operation
SwiftWebSocket copied to clipboard

close, end, error never called when socket is killed (non gracefully) at the other end

Open chadham opened this issue 7 years ago • 0 comments

On Swift 4+, connecting to a web socket on an esp8266. When I restart the ESP, the socket is obviously closed, but none of the events fire in the swift code. Tried with the event methods and delegate methods....

//~~
socket = WebSocket("ws://" + IP + ":" + String(port))
        socket.delegate = self
        socket.open()
        socket.event.error = { (e) in
            print("SOCKET ERROR \(e)")
        }
        socket.event.close = { code, reason, clean in
            print("closed due to code \(code), reason \(reason), clean \(clean)")
            }
        socket.event.end = {code, reason, clean, error  in
            print("end due to code \(code), reason \(reason), clean \(clean), error \(String(describing: error))")
        }
//~~


extension WebSocketClient: WebSocketDelegate{
    func webSocketMessageText(_ text: String) {
        NotificationCenter.default.post(Notification(name: Notification.Name(rawValue:Constants.WEBSocketMessageReceived),object: self,userInfo: ["message": text]))
    }
    
    func webSocketMessageData(_ data: Data) {
        print("Websocket received data \(data)")

    }
    
    func webSocketOpen() {
        print("WEBSocket opened to \(IP):\(port)")
        connected = true
        observer.connected(client: self)
    }
    
    func webSocketClose(_ code: Int, reason: String, wasClean: Bool) {
        print("WEBSocket closed to \(IP):\(port)")
        connected = false
        observer.disconnected(client: self)
    }
    
    func webSocketError(_ error: NSError) {
        print("WEBSocket error on \(IP):\(port)")
    }
    
    func webSocketEnd(_ code: Int, reason: String, wasClean: Bool, error: NSError?) {
        print("WEBSocket closed to \(IP):\(port)")
        connected = false
        observer.disconnected(client: self)
    }
    
}

chadham avatar Mar 24 '18 21:03 chadham