简体   繁体   English

服务器功能将对所有用户运行一次

[英]Server function to be ran once for all users

Good evening, 晚上好,

In my SignalR application I have a javascript timer that is ran for all users "simultaneously". 在我的SignalR应用程序中,我有一个javascript计时器“同时”为所有用户运行。 At the end of this timer, a server function is called, and this is where this problem starts. 在此计时器结束时,将调用服务器功能,这就是问题开始的地方。 As the function is called at the end of the timer, every connected user calls it at the same time, which is unnecessary because it will return the same output for all connected users. 由于该函数在计时器末尾被调用,因此每个连接的用户都同时调用它,这是不必要的,因为它将为所有连接的用户返回相同的输出。 Being a logically complex function, having the server run it unnecessarily for all users adds up to be a great resource waste. 作为逻辑上复杂的功能,让服务器不必要地为所有用户运行它加起来是极大的资源浪费。

How can I make it so that it is ran only once (maybe the first time it is called (until the next timer stops))? 如何使它仅运行一次(也许是第一次调用(直到下一个计时器停止))?

Thank you in advance 先感谢您

You could make use of GlobalHost.ConnectionManager.GetHubContext . 您可以使用GlobalHost.ConnectionManager.GetHubContext This will allow you to get any hub context and then trigger Clients.All.YourFunction on that context. 这将允许您获取任何中心上下文,然后在该上下文上触发Clients.All.YourFunction That will send send a message to all connected clients subscribed to that hub. 这将向订阅该集线器的所有连接的客户端发送一条消息。

You will need to have a background process that runs every at the time your JavaScript function fires (by the way, relying on all your clients to call a JavaScript function simultaneously is really not a good idea; different client locations and different machine performance will mean they're not likely to be simultaneous). 您将需要有一个在您的JavaScript函数启动时每次运行的后台进程(顺便说一句,依靠所有客户端同时调用JavaScript函数确实不是一个好主意;不同的客户端位置和不同的机器性能将意味着它们不太可能同时发生)。

The following is assuming that you're just running this on a single server. 以下假设您仅在单台服务器上运行它。 If you're going to be deploying this to a web farm, then you'll need to use a Database value to ensure you don't repeat the same work, or set up a particular server instance to be responsible for doing the calls (otherwise you'll end up with one call per server). 如果要将其部署到Web场,则需要使用Database值来确保您不重复相同的工作,或者设置特定的服务器实例来负责调用(否则,您最终将在每个服务器上打一个电话)。

Create a process that runs in the Background (I'm sticking with a simple thread here, I actually use HangFire for this, but this will suffice for the example), eg On App_Start 创建一个在后台运行的进程(我在这里使用一个简单的线程,我实际上为此使用HangFire,但这足以满足示例要求),例如在App_Start上

Thread thread = new Thread(new ThreadStart(YourFunction));
thread.Start();

Then create YourFunction which will be responsible for your client calls: 然后创建YourFunction ,它将负责您的客户端调用:

private bool Cancel = false;

private void YourFunction()
{
    do 
    {
        string foo = "Foo";

        IHubContext context = GlobalHost.ConnectionManager.GetHubContext<YourHub>();
        context.Clients.All.SendYourMessage(foo);

        Thread.Sleep(10000);
    } 
    while(!Cancel)
}

And then on the client, just handle the message from the hub: 然后在客户端上,只需处理来自中心的消息:

youyHub.client.sendYourMessage = function(message)
{
   // message == "Foo"
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM