实现signalr每隔几秒检查一次数据库中的数据 [英] Implementing signalr to check for data in database for every few seconds

查看:131
本文介绍了实现signalr每隔几秒检查一次数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用现有的Web Api应用程序实现了SingalR,其中SignalR集线器类将每隔几秒检查一次数据库,并根据连接ID更新连接的客户端。



这是我的集线器类:



I have Implemented SingalR with existing web Api Application, where SignalR hub class will check the database for every few seconds and update the clients which are connected based on connection Id.

here is my hub class :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using System.Threading.Tasks;
using VHC.Core.Business.Managers.Old;
using VHC.Core.Common.Requests;
using VHC.Core.Business.Interfaces;
using VHC.Core.ServiceProxy.ServiceProxies;

namespace VHC.Core.WebAPI
{
    public class NotifyHub : Hub
    {

        public static string ConnectionId { get; set; }
        public string NotificationResult { get; set; }

        //public NotifyHub()
        //{
    //}


        public void PushNotificationData(Int32 userId, Int16 userTypeId)
        {

            lock (this)
            {
                var request = new PushNotificationRequest
                {
                    FilterProperty = new Common.Filters.FilterProperty { Offset = 0, RecordLimit = 0, OrderBy = "datechecked desc" },
                    Filters = new List<Common.Filters.FilterObject> { new Common.Filters.FilterObject { LogicOperator = 0, ConditionOperator = 0, Function = 0, FieldName = "", FieldValue = "", FieldType = 0 } }
                };
                INotificationManager inotifity = new NotificationManager();
                var taskTimer = Task.Run(async () =>
                {
                    while (true)
                    {
                        //var serviceProxy = new NotificationServiceProxy();
                        // var NotificationResult = inotifity.PublishResultsAsync(request, userId, userTypeId);--check database

                        if (userId == 3134)
                        {
                            NotificationResult = "validated";
                           //Sending the server time to all the connected clients on the client method SendServerTime()
                            Clients.Client(ConnectionId).NotificationToClient(NotificationResult);
                        }
                        else
                        {
                           NotificationResult = "not validated";

                           //Sending the server time to all the connected clients on the client method SendServerTime()
                           Clients.Client(ConnectionId).NotificationToClient(NotificationResult);
                        }

                        //Delaying by 6 seconds.
                        await Task.Delay(6000);
                    }
                } );
            }
        }

        public override Task OnConnected()
        {
            ConnectionId = Context.ConnectionId;

            return base.OnConnected();
        }
    }
}







问题我是面对是,正如我们所知,Client Application将创建中心实例并建立连接。当我立刻运行客户端应用程序时,我没有遇到任何问题。当我在两个或多个并行运行客户端应用程序时,我遇到了一个问题,这意味着数据完全搞砸了。



例如:对于PushNotificationData方法,客户端应用程序将动态传递 userID 。在上面的代码中,我在 userID == 3143 时写了一个逻辑,响应应该'验证',否则'不验证'。



如果我在一个浏览器中运行一个应用程序,我每隔6秒收到一次响应,因为'已验证'



但如果我在两个或更多并行运行一个应用程序,我每6秒收到一次响应,因为有时'验证了'某些时间'未经过验证'。



我想有些事搞砸了。请帮助我。



我尝试了什么:



我尝试过锁定语句可以解决我的问题,但它没有。




Problem I'm facing is, As we know Client Application will create hub instance and connection will be established. When I'm running a client application at once, I didn't get any issue. When I'm running the client application at two or more parallely, I'm getting an issue which means data are completely messed up.

eg: for PushNotificationData method, Client application will pass the userID dynamically. In above code , I have written a logic when userID ==3143 ,the response should be 'validated', else 'not validated'.

if i'm running an application in one browser, I'm getting the response every 6 seconds as 'validated'

but if i'm running an application at two or more parallely,I'm getting the response every 6 seconds as sometime 'validated' some time 'not validated'.

I guess something messed up. kindly assist me.

What I have tried:

I tried lock statement can fix my issue, but it doesn't.

推荐答案

你好,



你能来吗?尝试



Hello,

Can you try

Clients.Caller.NotificationToClient(NotificationResult);





此外,如果你想映射用户和连接,你应该阅读将SignalR用户映射到连接| ASP.NET站点 [ ^ ]


这篇关于实现signalr每隔几秒检查一次数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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