c# - 如何使用 SignalR Hubs 从服务器向客户端发送消息

标签 c# signalr signalr-hub

我刚刚开始探索 signalR,我希望能够从服务器向所有客户端发送消息。

这是我的中心

 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);
        }
    }
}

这是我的客户页面

      <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>

这一切都很完美,我可以在 2 个不同的浏览器之间“聊天”。

接下来我要做的是启动一条从服务器到所有客户端的消息。

所以我尝试了这个。

 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");      
        }
    }
}

这似乎行不通。计时器工作,“aTimer_Elapsed”事件处理程序每​​ 3 秒运行一次,但聊天中心上的“发送”方法永远不会运行。

有什么想法吗?

最佳答案

我觉得应该是

 void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        var context = GlobalHost.ConnectionManager.GetHubContext<Chat>();
        context.Clients.All.addMessage("Hello");      
    }

相反。使用发送,您正在调用客户端用来调用服务器的方法...

关于c# - 如何使用 SignalR Hubs 从服务器向客户端发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12188029/

相关文章:

.net - 不在本地主机上时 SignalR 连接失败

c# - SignalR : Where to store them? 中的 session 数据

c# - 为什么 10 亿 * 100 == 12 亿?

task-parallel-library - 任务返回中心方法的用例

c# - 减少 LambdaExpression 签名

c# - WebAPI + OWIN + SignalR + Autofac

signalr - 为什么在通过 NuGet 更新到最新的 SignalR 库后 IAppBuilder 未定义?

javascript - SignalR MVC4 问题

c# - 如何使用 HttpWebRequest 进行摘要式身份验证?

c# - 我如何在 silverlight 中将 Xaml 转换为 Rtf?