您将需要使用集线器。
这是一个如何做到这一点的例子(在 0.5.3 中):
服务器端(集线器):
public class MyDashboard : Hub
{
/* Have this method called every few seconds via a timer */
public void SendLoad()
{
// Will call the "retrieveLoad" JS method on the clients
Clients.retrieveLoad(new {
CPU = 10.25, // Put something relevant here
Memory = "80%" // Again, put something relevant here
});
}
public double GetDiskSpace()
{
return 3829847; // You'd replace the number with something more relevant
}
}
客户端(javascript):
var dashboard = $.connection.myDashboard;
dashboard.retrieveLoad = function(info) {
console.log("The CPU is at: " + info.CPU);
console.log("The Memory is at: " + info.Memory);
}
$.connection.hub.start(function() {
// This is called once the hub has started, so we need to wire up our click event
$("#myButton").click(function() {
dashboard.getDiskSpace(function(space) {
console.log("The disk space is at: " + space);
});
});
});
希望这可以帮助!随时在我们的 JabbR 房间停下来与其他人讨论该框架:http: //jabbr.net/#/rooms/signalr
注意:请记住,如果您希望在集线器上保留任何内容(私有/公共变量等)以使该数据静态化。