locked
SignalR Hub method mapping mvc 5 serverside method. RRS feed

  • Question

  • User1000691596 posted

    Hello everyone,

    I developing an asp.net mvc 5 application where I need to use signalR. I have created the hub and I was able to connect with the hub. Now the main problem I'm facing how to map serverside methods with the hub. I am totally new with signalR if any can help me that will be a great help for me. Here is my code :

    HUB:
    
     [HubName("statusLog")]
        public class StatusLogHub : Hub
        {
            
            [HubMethodName("sendExportStatus")]
            public void SendExportStatus()
            {
                IHubContext context = GlobalHost.ConnectionManager.GetHubContext<StatusLogHub>();
                Clients.All.updateStatus();
            }
        }
    
    Repository : 
    
    public class EmailStatusLogRepository
        {
            EMailDBContext _ctx = new EMailDBContext();
    //I need to show this lstEmailStatus list in real time.        
            public IEnumerable<EmailStatusLog> GetExportStatus()
            {
                var lstEmailStatus = _ctx.emailStatusLogs.Where(x => x.IsActive == true && x.Date ==    DateTime.Now.ToString()).ToList();
                return lstEmailStatus;
            }
    
        }
    
    
    Controller :
    
    public ActionResult GetExportStatus()
            {
                EmailStatusLogRepository objEmailStatusRepository = new EmailStatusLogRepository();
                return PartialView("_exportedReportList", objEmailStatusRepository.GetExportStatus());
            }
    
    View:
    
     $("#btnExportStatus").click(function () {
    
                    $.ajax({
                        url: '@Url.Action("GetExportStatus")',
                        contentType: 'application/html ; charset:utf-8',
                        type: 'GET',
                        dataType: 'html'
                    }).success(function () {
                        var connection = $.hubConnection();
                        var hub = connection.createHubProxy("statusLog");
                        hub.on("updateStatus", function (statusUpdated) {
                            $('#hitCountValue').text(statusUpdated);
                        });
                        
                    });
                    // Declare a proxy to reference the hub.
    
                });
    
                
            function getExportStatus() {
                var tbl = $('#statusTable');
                $(function (result) {
                    tbl.empty().append(result);
                }).error(function () {
    
                });
            };
    


     

    Wednesday, March 16, 2016 3:49 AM

Answers

  • User61956409 posted

    Hi moinulmithu,

    Firstly, You had better to learn the basic usage of ASP.NET SignalR, it will be helpful for you use ASP.NET SignalR in your MVC project.

    http://www.asp.net/signalr/overview/getting-started

    Secondly, could you explained more about your requirements?

    Besides, as I said in this thread you posted, you define a hub method named “sendExportStatus”, but you don’t call this hub method from clients.

    http://forums.asp.net/p/2087786/6030143.aspx  

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 17, 2016 8:24 AM