使用c#的signalR向客户发送消息

时间:2021-09-15 16:27:59

I want to send some kind of timer to server to all connected clients. I was able to do this on button click. But what I really need is to send this timer every second automatically.

我想给所有连接的客户端发送一些定时器。我可以在点击按钮的时候做这个。但我真正需要的是每秒钟自动发送这个计时器。

HTML

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Signalr Chat Messenger</title>
    <script src="Scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.signalR-1.0.0-rc1.js" type="text/javascript"></script>
    <script src="signalr/hubs" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">

    <script type="text/javascript">
        $(function () {

            var IWannaChat = $.connection.myChatHub;

            IWannaChat.client.addMessage = function (message) {
                $('#listMessages').append('<li>' + message  + '</li>');
            };

            IWannaChat.client.addTest = function (timer) {
                $('#lblTimer').text(timer);
            };

            $("#SendMessage").click(function () {
                IWannaChat.server.send($('#txtMessage').val());
            });

            $.connection.hub.start();

        });
    </script>

    <div>
        <input type="text" id="txtMessage" />
        <input type="button" id="SendMessage" value="broadcast" /><asp:Label ID="lblTimer" runat="server" Text="timer"></asp:Label>
&nbsp;<ul id="listMessages">
        </ul>
    </div>
    </form>
</body>
</html>

and this is my HUB

这是我的中心

using System;
using Microsoft.AspNet.SignalR.Hubs;
using System.Threading;

namespace SignalRChat
{
    [HubName("myChatHub")]
    public class LetsChat : Hub
    {
        DateTime dateTime2 = DateTime.Now;
        string dateTime1 = "11.04.2016 15:49:00";   




        public void send(string message)
        {
            //Thread.Sleep(1000);
            var diffInSeconds = (Convert.ToDateTime(dateTime1) - dateTime2).TotalSeconds;

            Clients.All.addMessage(message);
            Clients.All.addTest(diffInSeconds.ToString());
            //Clients.All.addTest(dateTime1.ToString());
        }


    }
}

1 个解决方案

#1


2  

You can make use of System.Timers.Timer to do that:

你可以使用system . timer。定时器:

public class LetsChat : Hub
{
    private static readonly System.Timers.Timer _timer = new System.Timers.Timer();

    static LetsChat()
    {
        _timer.Interval = 1000;
        _timer.Elapsed += TimerElapsed;
        _timer.Start();
    }

    static void TimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        var hub = GlobalHost.ConnectionManager.GetHubContext<LetsChat>();

        var diffInSeconds = (Convert.ToDateTime(dateTime1) - dateTime2).TotalSeconds;

       hub.Clients.All.addMessage("Message");
       hub.Clients.All.addTest(diffInSeconds.ToString());
    }

    public void send(string message)
    {
        //Thread.Sleep(1000);
        var diffInSeconds = (Convert.ToDateTime(dateTime1) - dateTime2).TotalSeconds;

        Clients.All.addMessage(message);
        Clients.All.addTest(diffInSeconds.ToString());
        //Clients.All.addTest(dateTime1.ToString());
    }

}

#1


2  

You can make use of System.Timers.Timer to do that:

你可以使用system . timer。定时器:

public class LetsChat : Hub
{
    private static readonly System.Timers.Timer _timer = new System.Timers.Timer();

    static LetsChat()
    {
        _timer.Interval = 1000;
        _timer.Elapsed += TimerElapsed;
        _timer.Start();
    }

    static void TimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        var hub = GlobalHost.ConnectionManager.GetHubContext<LetsChat>();

        var diffInSeconds = (Convert.ToDateTime(dateTime1) - dateTime2).TotalSeconds;

       hub.Clients.All.addMessage("Message");
       hub.Clients.All.addTest(diffInSeconds.ToString());
    }

    public void send(string message)
    {
        //Thread.Sleep(1000);
        var diffInSeconds = (Convert.ToDateTime(dateTime1) - dateTime2).TotalSeconds;

        Clients.All.addMessage(message);
        Clients.All.addTest(diffInSeconds.ToString());
        //Clients.All.addTest(dateTime1.ToString());
    }

}