2014-06-13 122 views
0

我從Excel電子表格中導入記錄。我想用每條記錄的進度更新客戶端屏幕。我被告知SignalR是未來的方向。使用SignalR將代碼中的消息推送到客戶端

我已經開始使用ChatHub示例並將其剝離一點。

編輯... 看來,broadcastMessage功能正在工作,但奇怪的行爲。這裏是正在發生的事情的一個簡短的視頻:https://www.youtube.com/watch?v=wRqiyaQ2hD0

ASPX頁面

<form id="form2" runat="server"> 

    <ul id="progressReport"></ul> 

    <asp:Button Text="Push message" ID="btn" OnClick="btn_Click" runat="server" /> 

    <!--Script references. --> 
    <!--Reference the jQuery library. --> 
    <script src="Scripts/jquery-1.8.2.min.js"></script> 
    <!--Reference the SignalR library. --> 
    <script src="Scripts/jquery.signalR-2.0.3.min.js"></script> 
    <!--Reference the autogenerated SignalR hub script. --> 
    <script src="signalr/hubs"></script> 

    <!--Add script to update the page and send messages.--> 
    <script type="text/javascript"> 
     $(function() { 
      // Declare a proxy to reference the hub. 
      var chat = $.connection.chatHub; 
      // Create a function that the hub can call to broadcast messages. 
      chat.client.broadcastMessage = function (name, message) { 
       // Html encode display name and message. 
       var encodedName = $('<div />').text(name).html(); 
       var encodedMsg = $('<div />').text(message).html(); 
       // Add the message to the page. 
       $('#progressReport').append('<li><strong>' + encodedName 
        + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>'); 
      }; 

      // Start the connection. 
      $.connection.hub.start().done(function() { 
       chat.server.send($('#displayname').val(), $('#message').val()); 
      }); 
     }); 
    </script> 
</form> 

ASPX.CS

using SignalRChat; 
using System; 

namespace SignalR_Test 
{ 
    public partial class form2 : System.Web.UI.Page 
    { 
     protected void btn_Click(object sender, EventArgs e) 
     { 
      ChatHub ch = new ChatHub(); 
      for (int i = 0; i <= 10; i++) 
      { 
       ch.Send("Code Behind", i +" has been imported"); 
      } 
     } 
    } 
} 

ChatHub.CS

using Microsoft.AspNet.SignalR; 
namespace SignalRChat 
{ 
    public class ChatHub : Hub 
    { 
     public void Send(string name, string message) 
     { 
      var context = GlobalHost.ConnectionManager.GetHubContext<ChatHub>(); 
      // Call the broadcastMessage method to update clients. 
      context.Clients.All.broadcastMessage(name, message); 
     } 
    } 
} 

回答

0

你缺檔要求Startup班?見示例here

JS腳本版本(jquery.signalR-2.0.3.min.js,jquery-1.8.2.min.js)是否與'Scripts'目錄下的腳本相匹配?

+0

對不起,啓動類是存在的。我沒有調整過,所以忘了提及它! – wotney

+0

腳本版本如何(html與腳本目錄)? – st4hoo

+0

這些腳本都可以從我所能看到的內容中正確引用。我通過鏈接指向視頻來編輯我的問題,以顯示發生了什麼。 – wotney

2

嘗試增加給你的aspx頁面,並檢查它是否記錄的調用在JavaScript控制檯:

$.connection.hub.logging = true; 
$.connection.hub.start(); 

你的類被稱爲ChatHub和你正在做在客戶端側的connection.chatHub(注意小寫c大寫)。嘗試添加HubName屬性您Hub類:

[HubName("chatHub")] 
public class ChatHub : Hub 

同時,儘量增加~/到你的腳本源:

<script src="~/signalr/hubs"></script> 
+0

感謝您的幫助。控制檯報告沒有錯誤 - 最後一條消息是:SignalR:調用chathub.Send ...我發現有2個瀏覽器意外的事情發生。我認爲這與我從聊天演示開始的事實有關。 (video here:http://youtu.be/8pg5jV-nxMQ?hd=1) – wotney

+0

在該位置添加[HubName(「chatHub」)]會產生2個錯誤:無法找到類型或名稱空間名稱'HubNameAttribute' 無法找到類型或名稱空間名稱'HubName' – wotney

+0

使用Microsoft.AspNet.SignalR.Hubs將此添加到您的使用語句中;' – SOfanatic

相關問題