如何在signalr中以用户为基础向客户端发送响应 [英] How to send response to clients on user basis in signalr

查看:108
本文介绍了如何在signalr中以用户为基础向客户端发送响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

申请流程:



考虑客户端应用程序在不同的环境中运行,web Api应用程序在不同的环境中运行。



这是我的web api项目的集线器代码:

Application flow:

consider client application is running in separate environment, web Api application is running in separate environment.

here is my hub code for web api project :

public class NotifyHub : Hub
    {
        public NotifyHub()
        {
            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 } }
            };
            Int16 userId = 3134;
            Int16 usertypeid = 1;
            INotificationManager inotifity = new NotificationManager();
            var taskTimer = Task.Factory.StartNew(async () =>
            {
                while (true)
                {
                    //var serviceProxy = new NotificationServiceProxy();
                    var NotificationResult = inotifity.PublishResultsAsync(request, userId, usertypeid);
                    //Sending the server time to all the connected clients on the client method SendServerTime()
                    Clients.All.NotificationToClient(NotificationResult);

                    //Delaying by 60 seconds.
                    await Task.Delay(60000);
                }
            }, TaskCreationOptions.LongRunning
              );
        }
    }





来自您可以看到的上述代码, Int16 userId = 3134;



实际上,它应该是动态的。在客户端应用程序中,如果用户ID 3134已登录,则必须发送属于3134的响应数据。



响应应与客户端上登录的用户相对应应用程序。



我怎样才能实现它。



连接集线器的客户端应用程序代码:



from the above code you can see , Int16 userId = 3134;

Actually ,it should be dynamic. In a client application if userid 3134 is logged in, i have to send response data which is belongs to 3134.

response should be respective to the logged in user on client application.

how can i achieve it.

Client application code to connect the Hub:

var app = angular.module('app', []);
app.value('$', $);

app.factory('NotificationService', function ($, $rootScope) {
    return {
        proxy: null,
        initialize: function (NotificationToClientCallback) {
            //Getting the connection object
            var connection = $.hubConnection("http://localhost:50728/");

            //Creating proxy
            this.proxy = connection.createHubProxy('NotifyHub');

            //Attaching a callback to handle NotificationToClient client call
            this.proxy.on('NotificationToClient', function (message) {
                $rootScope.$apply(function () {
                    NotificationToClientCallback(message);
                });
            });

            //Starting connection
            connection.start({ jsonp: true })
           .done(function () { console.log('Now connected, connection ID=' + connection.id); })
           .fail(function () { console.log('Could not connect'); });

        },
        sendRequest: function (callback) {
            //Invoking SendNoficationRequest method defined in hub
            this.proxy.invoke('SendNoficationRequest');
        }
    }
}).controller('NotificationCtrl', ['$scope', 'NotificationService', function NotificationCtrl($scope, NotificationService) {
        $scope.text = "";

        $scope.SendNoficationRequest = function () {
            NotificationService.sendRequest();
        }

        updateDisplayData = function (text) {
            $scope.text =  text;
        }
        NotificationService.initialize(updateDisplayData);

    }]);





我尝试了什么:



我搜索过如何传递参数s for hub,但我无法继续进行。



What I have tried:

I have searched about how to pass parameters for hub, but I cant able to proceed further.

推荐答案

',


);

app.factory(' NotificationService' function



这篇关于如何在signalr中以用户为基础向客户端发送响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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