Get full URL inside SignalR hub

I am developing a user tracking solution using SignalR, as a fun project to learn SignalR for ASP.NET MVC applications.

Currently, I can track registered users and how long they have been on a particular page. If they go to another page, I also track this timer, which SignalR updates to reset ... Many other functions are implemented or partially implemented.

The problem I am facing is how to get the full Controller / Action / Parameters url inside the SignalR hub?

When I use HttpContext.Current.Request.Url , the url is always / signalr / connect .

NOTE:

 var hub = $.connection.myHub; $.connection.hub.start(); 

located in _Layout.cshtml.

UPDATE:

I tried to use

 var location = '@HttpContext.Current.Request.Url'; var hub = $.connection.myHub; $.connection.hub.start().done(function () { hub.setLocation(location); }); 

And the location is passed correctly, but I need it in the Connect () job no later than. Can this be done?

UPDATE 2:

This approach does not work

 var hub = $.connection.myHub; $.connection.hub.start(function(){hub.setLocation(location)}); 

How Connect() called.

In my hub, I have several methods, but I would like to pass the value (in my case location) to Connect() , is this possible?

 public class MyHub : Hub, IDisconnect, IConnected { public Task Connect() { //do stuff here //and i would like to have the **location** value } public Task Disconnect() { //do stuff here } } 

Update 3

Use a QueryString to pass data before Connect() appears.

 var location = '@HttpContext.Current.Request.Url'; var hub = $.connection.myHub; $.connection.hub.qs = "location= + location; $.connection.hub.start(); 
+8
asp.net-mvc-4 signalr signalr-hub
source share
3 answers

Transferring data, such as your location value, to Connect () is possible using the querystring parameter: SignalR: how to send data to IConnected.Connect ()

+4
source share

You can pass it from your client js call to your hub as a parameter.

+1
source share

Using the query string is not very safe, so a hacker can fake JS code and send you the wrong location, destroying any logic that you have.

You can try to get this from owin-enviromment variables

var underlyingHttpContext = Context.Request.Environment[typeof(HttpContextBase).FullName] as HttpContextBase;

Then extract everything you need.

He will work on IIS, for hosting other than IIS, look for other OWIN materials https://github.com/aspnet/AspNetKatana/wiki/OWIN-Keys

+1
source share

All Articles