如何在没有客户端输入的情况下自动定期从Signalr接收消息到所有客户端?

时间:2016-11-24 15:31:32

标签: javascript c# signalr timedelay clients

我是SignalR的新手。我需要在没有客户端输入的情况下自动将SignalR的消息从SignalR发送到所有连接的客户端吗?

上述过程必须以递归方式重复进行?

有可能吗?

如果没有客户端输入,SignalR可以反复自动向客户端发送消息吗?

这是我的JavaScript cleint代码:

$(function () {
    var chat = $.connection.timehub;
    $.connection.hub.start();
    chat.client.broadcastMessage = function (current) {
        var now = current;
        console.log(current);
        $('div.container').append('<p><strong>' + now + '</strong></p>');
    }
};

这是我的Timehub

public class timehub : Hub
{
    public void Send(string current)
    {
        current = DateTime.Now.ToString("HH:mm:ss:tt");
        Clients.All.broadcastMessage(current);

        System.Threading.Thread.Sleep(5000);
        Send(current);
    }
}

这是我的Owin Startup Class:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.MapSignalR();

    }
}

有人能为我提供解决方案吗?

1 个答案:

答案 0 :(得分:1)

如果你像现在一样递归地调用with test_data ( license_number ) as ( select '111246544' from dual union all select '999999999' from dual union all select '123456789' from dual union all select 'AAAAAAAAA' from dual union all select 'A12345667' from dual ) -- end of test data; solution (SQL query) begins below this line select t.license_number, length(t.license_number) - min(coalesce(length(replace(t.license_number, c.column_value, '')), 0)) as max_count from test_data t cross join table(sys.odcivarchar2list( '0','1','2','3','4','5','6','7','8','9','0','A','B','C','D','E','F')) c group by t.license_number ; LICENSE_NUMBER MAX_COUNT -------------- --------- AAAAAAAAA 9 123456789 1 A12345667 2 999999999 9 111246544 3 方法,你将得到一个stackoverflow异常。只需将代码包含在Send()循环中的方法中:

while(true)

我建议将public class timehub : Hub { public void Send() { while(true) { var current = DateTime.Now.ToString("HH:mm:ss:tt"); Clients.All.broadcastMessage(current); System.Threading.Thread.Sleep(5000); } } } 方法移动到另一个线程,因为当前线程将在此while循环中永远停滞。