C# 如何使用 SignalR 集线器将消息从服务器发送到客户端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12188029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I send messages from server to client using SignalR Hubs
提问by David Kethel
I am just starting to explore signalR and I would like to able to send messages from the server to all clients.
我刚刚开始探索 signalR,我希望能够从服务器向所有客户端发送消息。
Here is my Hub
这是我的集线器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SignalR;
using SignalR.Hubs;
using SignalR.Hosting.Common;
using SignalR.Hosting.AspNet;
using System.Threading.Tasks;
namespace MvcApplication1
{
public class Chat : Hub
{
public void Send(String message)
{
// Call the addMessage methods on all clients
Clients.addMessage(message);
}
}
}
Here is my client Page
这是我的客户页面
<script type="text/javascript">
$(function () {
//Proxy created on the fly
var chat = $.connection.chat;
// Declare a function on the chat hub so the server can invoke it
chat.addMessage = function (message) {
$("#messages").append("<li>" + message + "</li>");
};
$("#broadcast").click(function () {
// call the chat method on the server
chat.send($("#msg").val());
});
$.connection.hub.start();
});
</script>
}
<input type="text" id="msg" />
<input type="button" id="broadcast" value="broadcast" />
<ul id="messages" class="round">
</ul>
This all works perfectly, I am able to "chat" between 2 different browsers.
这一切都很完美,我能够在 2 个不同的浏览器之间“聊天”。
The next thing I want to do is initiate a message from the server to all clients.
我要做的下一件事是从服务器向所有客户端发起一条消息。
So I tried this.
所以我尝试了这个。
using SignalR;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System;
using System.Web.Routing;
using SignalR;
using SignalR.Hubs;
namespace MvcApplication1
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
var aTimer = new System.Timers.Timer(1000);
aTimer.Elapsed += aTimer_Elapsed;
aTimer.Interval = 3000;
aTimer.Enabled = true;
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
var context = GlobalHost.ConnectionManager.GetHubContext<Chat>();
context.Clients.Send("Hello");
}
}
}
This doesn't seem to work. The Timer works, The "aTimer_Elapsed" event handeler runs every 3 seconds but the "Send" method on the chat hub is never run.
这似乎不起作用。计时器工作,“aTimer_Elapsed”事件处理程序每 3 秒运行一次,但聊天中心上的“发送”方法从不运行。
Any ideas?
有任何想法吗?
采纳答案by eddo
I think it should be
我认为应该是
void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
var context = GlobalHost.ConnectionManager.GetHubContext<Chat>();
context.Clients.All.addMessage("Hello");
}
instead. With Send you are calling the method used by the client to call the server...
反而。使用 Send 您正在调用客户端使用的方法来调用服务器...
回答by 6134548
Yes you must set that line to:
是的,您必须将该行设置为:
void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
var context = GlobalHost.ConnectionManager.GetHubContext<Chat>();
context.Clients.All.addMessage("Hello");
}
However this is only half way and still wont work.
然而,这只是一半,仍然行不通。
In your Js you need to write:
在您的 Js 中,您需要编写:
$(function () {
//Proxy created on the fly
var chat = $.connection.chat;
// Declare a function on the chat hub so the server can invoke it
chat.client.addMessage = function (message) {
$("#messages").append("<li>" + message + "</li>");
};
$("#broadcast").click(function () {
// call the chat method on the server
chat.client.addMessage($("#msg").val());
});
$.connection.hub.start();
});
I added the chat.clientthis will add a client-side hub method that the server will call.
我添加了chat.client这将添加服务器将调用的客户端集线器方法。

