SignalRSwift .on method not working for receiving messages

Viewed 32

I am using SignalRwift Library for chat application. Library link: https://github.com/AutosoftDMS/SignalR-Swift

This signalR library is for asp.net, not for asp.net core. Both are different framework.

This library is supported only for asp.net

Right now, I'm working on a chat application. To send data on server, I am using API (successful) but I am unable to receive the data. I am using .on method of signal to get the data.

 hubConnection = HubConnection(withUrl: "https://test.scrm.com")
    chat = self.hubConnection.createHubProxy(hubName: "NotificationHub")
    
    _ = chat?.on(eventName:"broadcastMessage") { (args) in
         if let name = args[0] as? String, let id = args[1] as? Int, let message = args[2] as? String {
                    print("Name: \(name), VisitorID: \(id), message: \(message)")
                }
     }
    
    hubConnection.started = {
        self.typingLbl.text = "Connected"
    }
    
    hubConnection.reconnecting = {
        self.typingLbl.text = "Connected"
        print("Reconnecting...")
    }
    
    hubConnection.reconnected = {
        self.typingLbl.text = "Reconnected, connection ID: \(self.hubConnection.connectionId ?? " ")"
        print("Reconnected.")
    }
    
    hubConnection.closed = {
        self.typingLbl.text = "Hub Disconnected"
        print("Hub Disconnected")
    }
    
    hubConnection.connectionSlow = {
        self.typingLbl.text = "Connection Slow"
        print("Connection slow...") }
    
    hubConnection.error = { error in
        let anError = error as NSError
        if anError.code == NSURLErrorTimedOut {
            self.hubConnection.start()
        }
    }
    
    hubConnection.start()
   

Here is how I'm sending messages to the server. The backend is asp.net and I'm successfully receiving messages at my backend. Api handles the "send" response so I do not need to invoke send method.

 @objc func sendMessage() {
    if let messageTxt = messageTxtField.text {
       
        self.saveVisitorChat(visitorSessionID: user?.visitorSession.id ?? 0, visitorId: user?.id ?? 1, message: messageTxt)
        
        message.append(messageTxt)
        print("Messages \(message)")
    }
}

}

now when I reply from the backend, I do not receive any message in

_ = chat?.on(eventName:"broadcastMessage") { (args) in
     if let name = args[0] as? String, let id = args[1] as? Int, let message = args[2] as? String {
                print("Name: \(name), VisitorID: \(id), message: \(message)")
            }
 }

and I have called this in viewdidload. Anyone could help out? Most of the topics are related to SwiftR library for asp.net core

Backend:

context.Clients.All.broadcastMessage(response.LoggedInUserName, reply.WcVisitorId, reply.ReplyMessage);

 myHub.client.broadcastMessage = function (name, id, message) {
        debugger
        if (localStorage.getItem("vId") == id) {
            var time = new Date();
            $("#chat-body").append(
                '<div class="chat-message" id="">' +
                '<div class="chat-message-content-w">' +
                ' <div class="chat-message-content" id="server-reply">' +
                '<div class="chatusern"><strong>' + name + '</strong></div>' +
                '<span>' + createTextLinks(message) + '</span>' +
                '<div class="chat-message-date" id="message-time">' +
                '<p>' + time.toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', hour12: true }) + '</p>' +
                '</div>' +
                '</div>' +
                '</div>' +
                '</div>');
            $("#testThis").animate({ scrollTop: $(document).height() + 5000000 }, "slow");
            return false;
        }
    };
0 Answers
Related