2012-08-28 98 views
1

我有发票进口商枢纽,像这样:SignalR触发事件的客户端在MVC3

public class ImporterHub : Hub, IDisconnect, IConnected 
{ 

    public void InvoiceImported(InvoiceImportedMessage message) 
    { 
     Clients["importer"].InvoiceImported(message); 
    } 

    public void FileImported(FileImportedMessage message) 
    { 
     Clients["importer"].FileImported(message); 
    } 

    public System.Threading.Tasks.Task Disconnect() 
    { 
     return Clients["importer"].leave(Context.ConnectionId, DateTime.Now.ToString()); 
    } 

    public System.Threading.Tasks.Task Connect() 
    { 
     return Clients["importer"].joined(Context.ConnectionId, DateTime.Now.ToString()); 
    } 

    public System.Threading.Tasks.Task Reconnect(IEnumerable<string> groups) 
    { 
     return Clients["importer"].rejoined(Context.ConnectionId, DateTime.Now.ToString()); 
    } 
} 

在我的控制,我捕捉事件长时间运行的导入过程中,像这样:

[HttpPost] 
    public ActionResult Index(IndexModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      try 
      { 
       model.NotificationRecipient = model.NotificationRecipient.Replace(';', ','); 
       ImportConfiguration config = new ImportConfiguration() 
       { 
        BatchId = model.BatchId, 
        ReportRecipients = model.NotificationRecipient.Split(',').Select(c => c.Trim()) 
       }; 
       var context = GlobalHost.ConnectionManager.GetHubContext<ImporterHub>(); 
       context.Groups.Add(this.Session.SessionID, "importer"); 
       System.Threading.ThreadPool.QueueUserWorkItem(foo => LaunchFileImporter(config)); 
       Log.InfoFormat("Queued the ImportProcessor to process invoices. Send Notification: {0} Email Recipient: {1}", 
        model.SendNotification, model.NotificationRecipient); 
       TempData["message"] = "The import processor job has been started."; 
       //return RedirectToAction("Index", "Home"); 
      } 
      catch (Exception ex) 
      { 
       Log.Error("Failed to properly queue the invoice import job.", ex); 
       ModelState.AddModelError("", ex.Message); 
      } 
     } 

    private void LaunchFileImporter(ImportConfiguration config) 
    { 
     using (var processor = new ImportProcessor()) 
     { 
      processor.OnFileProcessed += new InvoiceFileProcessing(InvoiceFileProcessingHandler); 
      processor.OnInvoiceProcessed += new InvoiceSubmitted(InvoiceSubmittedHandler); 
      processor.Execute(config); 
     } 
    } 

    private void InvoiceSubmittedHandler(object sender, InvoiceSubmittedEventArgs e) 
    { 
     var context = GlobalHost.ConnectionManager.GetHubContext<ImporterHub>(); 
     var message = new InvoiceImportedMessage() 
     { 
      FileName = e.FileName, 
      TotalErrorsInFileProcessed = e.TotalErrors, 
      TotalInvoicesInFileProcessed = e.TotalInvoices 
     }; 
     context.Clients["importer"].InvoiceImported(message); 
    } 
    private void InvoiceCollectionSubmittedHandler(object sender, InvoiceCollectionSubmittedEventArgs e) 
    { 
    } 
    private void InvoiceFileProcessingHandler(object sender, InvoiceFileProcessingEventArgs e) 
    { 
     var context = GlobalHost.ConnectionManager.GetHubContext<ImporterHub>(); 
     var message = new FileImportedMessage() 
     { 
      FileName = e.FileName 
     }; 
     context.Clients["importer"].FileImported(message); 
    } 

我在我的进口商查看下面的脚本:

<script type="text/javascript"> 
    jQuery.connection.hub.logging = true; 
    var importerHub = jQuery.connection.importerHub; 

    importerHub.InvoiceImported = function (message) { 
     jQuery('#' + message.FileName + '_Invoices').text(message.TotalInvoicesInFileProcessed); 
     jQuery('#' + message.FileName + '_Errors').text(message.TotalErrorsInFileProcessed); 
    }; 

    importerHub.FileImported = function (message) { 
     jQuery('#' + message.FileName + '_Processed').text('Done'); 
    }; 

    jQuery.connection.hub.start(); 
</script> 

我EXPE什么导致发生:

我期待的服务器端事件触发,这将发送消息到客户端, 这反过来会触发事件来更新导入过程的状态。

什么似乎要发生的事情:

所有服务器端事件触发,一切都很好。 signalR库似乎是正确初始化的,但事件永远不会触发,我从来没有将更新显示在屏幕上。

我在做什么错?这是我第一次尝试使用signalR库,所以完全有可能我做错了所有事情。

+0

,这有什么区别,如果你删除'[“进口商”]'参数,即'context.Clients .InvoiceImported(“message”);' – paul

+0

我试过了,谢谢;它似乎没有什么区别。 –

+0

SessionID?为什么要将SessionID传递给Groups.Add?这是错误的。 – davidfowl

回答

1

相信您的问题是,你的客户端枢纽事件被命名为使用init-帽和SignalR的默认行为是把那些INIT-下发布到客户端时,与常见的JavaScript约定一致。试着改变你的集线器事件注册此:

importerHub.invoiceImported = function (message) { 

importerHub.fileImported = function (message) { 
控制器
+0

我确实改变了这一点,现在看起来事件已经发生。现在我所要做的就是弄清楚为什么它没有更新我的DOM。谢谢! –