adamhartford / SwiftR

Swift client for SignalR (iOS and Mac)
MIT License
174 stars 74 forks source link

Hub.on is not getting called in Swift 4 :( #103

Open rajeevfp opened 6 years ago

rajeevfp commented 6 years ago

Hi I'm using SwiftR (0.14.0) in swift 4 Xcode 9 . The following connecting to server but hub.on is not getting called where i need to get response. Please help me to resolve this. Thanks

        hubConnection = SignalR("http://chat.friends.com/")
        hubConnection.useWKWebView = false
        let bearerKey = UserDefaults.standard.string(forKey: "BearerKey")! as NSString
        hubConnection?.headers = ["Authorization": "Bearer "+(bearerKey as String)]

        simpleHub = Hub("MobileChatHub")
        complexHub = Hub("MobileChatHub")

        hubConnection.addHub(simpleHub)
        hubConnection.addHub(complexHub)

        simpleHub.on("ChatContactsResponse") { args in
            let message = args![0] as! String
            let detail = args![1] as! String
            print("Message: \(message)\nDetail: \(detail)\n")
        }

        complexHub.on("ChatContactsResponse") { args in
            let m: AnyObject = args![0] as AnyObject!
            print(m)
        }

        // SignalR events

        hubConnection.starting = { [weak self] in
            print("Starting...")
        }

        hubConnection.reconnecting = { [weak self] in
            print("Reconnecting...")

        }

        hubConnection.connected = { [weak self] in
            print("Connected. Connection ID: \(String(describing: self!.hubConnection.connectionID))")
            do {
                try self?.simpleHub.invoke("GetChatContacts", arguments: [false])
            } catch {
                print(error)
            }
        }

        hubConnection.reconnected = { [weak self] in
            print("Reconnected. Connection ID: \(String(describing: self!.hubConnection.connectionID))")

        }

        hubConnection.disconnected = { [weak self] in
            print("Disconnected.")
        }

        hubConnection.connectionSlow = { print("Connection slow...") }

        hubConnection.error = { [weak self] error in
            print("Error: \(String(describing: error))")

            if let source = error?["source"] as? String , source == "TimeoutException" {
                print("Connection timed out. Restarting...")
                self?.hubConnection.start()
            }
        }

        hubConnection.start()
cavoixanha commented 6 years ago

Have you found a solution?

munsifhayat commented 6 years ago

Any solution ?

cavoixanha commented 6 years ago

Can server only send complexHub.on("event") to connectionId register, should you can't catch this event! Before I also faulty this! Fix by use hub.invoke('connect', username);

munsifhayat commented 6 years ago

For me its working pretty clean now. I think their is no issues in case of swift 4 and xCode 9.