如何使用SignalR集线器从服务器向客户端发送消息 [英] How do I send messages from server to client using SignalR Hubs

查看:335
本文介绍了如何使用SignalR集线器从服务器向客户端发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am just starting to explore signalR and I would like to able to send messages from the server to all clients.

这是我的集线器

 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个不同的浏览器之间聊天。

This all works perfectly, I am able to "chat" between 2 different browsers.

我接下来要做的是从服务器向所有客户端发起一条消息。

The next thing I want to do is initiate a message from the server to all clients.

所以我尝试了这个。

 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秒运行一次,但聊天中心上的 Send方法从不运行。

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.

有什么想法吗?

推荐答案

我认为应该是

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

这篇关于如何使用SignalR集线器从服务器向客户端发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆