Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :
C#
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
              );
        }
    }


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:
JavaScript
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);

    }]);


What I have tried:

I have searched about how to pass parameters for hub, but I cant able to proceed further.
Posted
Updated 10-Dec-16 22:36pm

1 solution

Hi,

In the Hub properties you have a Context property and you can access the current user who send the request like this :
C#
Int16 userId = Context.User.Identity.Name;
This will work fine if you use Forms Authentification Mode to save the connected user ID in your Login method like this :
C#
FormsAuthentication.SetAuthCookie(model.UserId.ToString(), false);
This code creates an authentication ticket for the supplied user name and adds it to the cookies collection.

Don't forget to set this in your webconfig file to specify that you are using Forms Authentification Mode
XML
<system.web>
  <authentication mode="Forms">
    <forms loginUrl="~/Account/Login" timeout="2880" />
  </authentication>
</system.web>

How to pass parameters for hub ?

I thank you can pass data to the Hub from client pass arguments to method like this.
JavaScript
proxy.invoke('methodName',arguments)
If you want the data to be available to the whole session you can store it in cookies and retrieve it in the Hub by the Context.RequestCookies property.


I hope it's will help. ;)

Reference :
ASP.NET SignalR Hubs API Guide - JavaScript Client | The ASP.NET Site[^]
 
Share this answer
 
v2
Comments
King Fisher 19-Dec-16 13:57pm    
thanks, I just passed the userId as parameter
Jean-Claude ADIBA 19-Dec-16 15:42pm    
Great. if it help you can you mark my answer as approved solution.
King Fisher 20-Dec-16 0:13am    
I just included the reference , thanks :) can you pls look into this?
new question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900