2016-03-06 136 views
0

啓動類:無法使用signalr將消息發送到客戶端在Firefox

using Owin; 
using Microsoft.Owin; 
using Microsoft.AspNet.SignalR; 
[assembly: OwinStartup(typeof(ESimSolFinancial.Startup))] 
namespace ESimSolFinancial 
{ 
    public class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      // Any connection or hub wire up and configuration should go here 
      app.MapSignalR("/signalr", new HubConfiguration()); 
     } 
    } 
} 

集中器類:在一個js文件

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading; 
using System.Web; 
using Microsoft.AspNet.SignalR; 

namespace ESimSolFinancial.Hubs 
{ 
    public class ProgressHub : Hub 
    { 

     public static void SendMessage(string msg,int nCount) 
     { 
      var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>(); 
      hubContext.Clients.All.broadcastMessage(string.Format(msg), nCount); 
     } 
    } 
} 

我有這樣的功能:

function InitializeProgressBar() { 
    ProgressBarShow(); 
    var progressNotifier = $.connection.progressHub; 

    progressNotifier.client.broadcastMessage = function (message, count) { 
     UpdateProgressBar(message, count); 
    }; 
    $.connection.hub.url = "/signalr"; 
    $.connection.hub.start({ transport: ['webSockets', 'longPolling'] }).done(function() { 
     //progressNotifier.client.broadcastMessage('Opening Connection',0); 
    }); 
} 

它被稱爲點擊事件

點擊e通氣調用一個動作結果,其中我有這樣的代碼:

Thread.Sleep(5000); 
ProgressHub.SendMessage("Getting Ledger Data", 10); 

到後長過程完成更新進度。

郵件在Chrome和IE中正確發送,但firefox沒有收到相應的消息。 UpdateProgressBar(message, count);不會被調用。它有時會在我打開螢火蟲並長時間停止執行時有效。

我認爲這是因爲漫長的過程不夠長,所以我增加了睡眠時間,但事實並非如此。

我不明白我在這裏做錯了什麼。任何幫助表示讚賞。

更新: actionresult是單擊事件所在的頁面。它似乎只在應用程序剛剛啓動後才起作用。之後,Firefox拒絕接收這些消息。

+0

你接收Firefox的控制檯任何錯誤時,就是所謂的單擊事件? –

+0

火狐不能建立在WS與服務器的連接://本地主機:2309/signalr /連接運輸=的WebSockets&clientProtocol = 1.5&connectionToken = mOqZQ0uzyM8ih51WabnKjKDBbl3B585Tiij5FpIGE7GnvSOSe7CMd0sKLSAP6Zf7waRYBIH8%2BgsKuqDumuSIhMhLx3C7%2Br5C7sLoBlzq2d%2BO1K0tVr6JKOTuLVK3LRyG&connectionData =%5B%7B%22name%22%3A% 22progresshub%22%7D%5D&TID = 4。 它顯示第二次連接令牌很長。這是什麼意思? –

+0

如果WebSocket傳輸的協商失敗並且使用另一個傳輸器,則可以在Firebug中看到此錯誤消息。此行爲是設計使然。 –

回答

0

有你在JS

progressNotifier.server.SendMessage(msg,count); 

您可以修改客戶端代碼調用服務器功能點擊事件......通過調用服務器功能

function InitializeProgressBar() { 
    ProgressBarShow(); 
    var progressNotifier = $.connection.progressHub; 

    progressNotifier.client.broadcastMessage = function (message, count) { 
     UpdateProgressBar(message, count); 
    }; 
    $.connection.hub.url = "/signalr"; 
    $.connection.hub.start({ transport: ['webSockets', 'longPolling'] 
     }).done(function() { 

    //Call to server function(SendMessage) made .This fuction broadcasts to all clients 
    //by calling client side(broadcast message) function of all users 
    progressNotifier.server.SendMessage(msg,count); 

     //progressNotifier.client.broadcastMessage('Opening Connection',0); 
    }); 
} 

請嘗試this.I不能在運行程序一個應用程序。但是這就是信號R的工作方式。一個客戶端在一個事件上調用服務器功能(或者服務器在沒有呼叫的情況下回復,但是有一些邏輯),服務器向選定的組播發實時通知,或者選定的用戶或全部用戶(在客戶端) 。

$('#id').click(function(){ 
    progressNotifier.server.SendMessage(msg,count); 
    }); 

服務器函數被調用

+0

'InitializeProgressBar()'這個函數是通過點擊事件調用 –

+0

嘗試從InitializeProgressBar()調用服務器函數SendMessage()。一個客戶端必須調用服務器函數,只有這樣服務器纔可以實時通知所有客戶端。 –

相關問題