2014-04-06 51 views
0

Im使用SignalR創建報告系統。我使用了一個已開始的項目,但必須開發它以滿足我的需求。當新用戶登錄時,系統應顯示先前由其他用戶編寫的消息,但系統顯示消息混亂且消息(目標)的一部分未定義。想知道我如何解決這個問題,以便消息顯示。它應該是這樣的:以錯誤順序顯示的緩存消息SignalR

切爾西VS阿森納(3) - (0):唧唧歪歪

但這樣表示:

BLA BLA blaChelsea VS阿森納:未定義

守則如下:

服務器端:

public void SendMessageToAll(string goal, string userName, string message) 
    { 
     // store last 100 messages in cache 
     AddMessageinCache(goal, userName, message); 

     // Broad cast message 
     Clients.All.messageReceived(goal, userName, message); 
    } 
} 

的JavaScript

function registerClientMethods(chatHub) { 

     // Calls when user successfully logged in 
     chatHub.client.onConnected = function (id, teamNames, allUsers, updates) { 

      $('#hdId').val(id); 
      $('#hdUserName').val(teamNames); 
      $('#spanUser').html(teamNames); 

      // Add Existing Messages 
      for (i = 0; i < updates.length; i++) { 

       AddMessage(updates[i].Goal, updates[i].TeamNames, updates[i].Message); 
       var view = AddMessage; 
      } 
     } 

     // On New User Connected 
     chatHub.client.onNewUserConnected = function (id, name) { 

      var homeScore = $('#homeGoal').val(); 
      var awayScore = $('#awayGoal').val(); 

      var goal = "(" + homeScore + ") - (" + awayScore + ")"; 

      AddUser(chatHub, id, name); 
      AddScore(chatHub, id, name, goal); 
     } 

     chatHub.client.messageReceived = function (goal, teamNames, updates) { 

      AddMessage(goal, teamNames, updates); 
      var view = AddMessage; 
     } 

    } 

    function AddMessage(goal, teamNames, updates) { 

     var homeScore = $('#homeGoal').val(); 
     var awayScore = $('#awayGoal').val(); 

     $('#divChatWindow').append('<div class="message"><span class="userName">' + teamNames + goal + '</span>: ' + updates + '</div>'); 

    } 

回答

0

在初始讀取,它看起來像你的名單可能每次都被覆蓋掉了輪轂得到instanciated-集線器是短暫的,併爲每個連接都重新創建。在集線器之外移動列表聲明,或者在分配給它之前測試列表爲空。