Signalr calls a specific client from outside the hub

I know the Chris Fulstow log4net.signalr project , this is a great idea if you want a non-working log, since it logs all messages from all requests. I would like to have something that recognizes the log messages by the request coming from them and is sent back to the appropriate browser.

Here is what I did in the application:

public class SignalRHubAppender:AppenderSkeleton { protected override void Append(log4net.Core.LoggingEvent loggingEvent) { if (HttpContext.Current != null) { var cookie = HttpContext.Current.Request.Cookies["log-id"]; if (null != cookie) { var formattedEvent = RenderLoggingEvent(loggingEvent); var context = GlobalHost.ConnectionManager.GetHubContext<Log4NetHub>(); context.Clients[cookie.Value].onLog(new { Message = formattedEvent, Event = loggingEvent }); } } } } 

I am trying to attach a session id to a cookie, but this does not work on the same computer because the cookie is overwritten. here is the code that I use on the client to attach the event:

 //start hubs $.connection.hub.start() .done(function () { console.log("hub subsystem running..."); console.log("hub connection id=" + $.connection.hub.id); $.cookie("log-id", $.connection.hub.id); log4netHub.listen(); }); 

As a result, only the last connected page displays log messages. I would like to know if there are any strategies to have the current connection identifier from the browser that initiates the current request, if one exists. I am also interested to know if there is a better design to achieve registration in the browser.

EDIT

I could create a cookie with a conditional name (e.g. log-id-someguid), but I'm wondering if there is anything smarter.

BOUNTY I decided to start a generosity on this issue, and I will additionally ask about architecture to find out if my strategy makes sense or not. I doubt that I use the hub in one โ€œdirectionโ€ from server to client, and I use it to register actions that are not related to calls to the hub, but from other requests (potentially requests raised on other hubs), which is the right approach, having as a target the browser visible log4net appender?

+4
source share
1 answer

The idea of โ€‹โ€‹how to properly configure the correct browser instance / tab, even when multiple tabs are open in the same SPA, is to distinguish them through Url. One of the possible ways to implement this is to redirect them at first access from http://foo.com to http://foo.com/hhd83hd8hd8dh3 , each time randomly generated. This URL rewriting can be done in other ways, but itโ€™s just a way to illustrate the problem. Thus, the application will be able to check the source Url, and with Url, through some mapping that you keep on the server side, you can determine the correct SignalR ConnectionId. Implementation details may vary, but the basic idea is this. By tracking some of the additional information available in the HttpContext since you first connected, you can also create additional strategies to prevent any hijacking.

About your architecture, I can say that this is exactly how I used it in ElmahR . I have messages coming from outside the notification center (errors sent from other web applications), and I broadcast all the clients connected to this hub (and subscribing to certain groups): it works fine.

I am not an authoritative source, but I also assume that such an architecture is in order, even with multiple hubs, because nodes at the end of the day are just an abstraction through a (one) persistent connection that allows you to group messages in contexts. Behind the scenes (I simplify) you only have constant communication with the messages going back and forth, so that regardless of the structure of the node that you define on top of it (which is just to help you organize things), you still insist on this connection, so you cannot do any harm.

SignalR is good at doing two things: mass broadcasting (Clients) and individual communication (Caller). While you are not trying to do strange things, such as building links to server links to specific subscribers, you should be fine, regardless of the number of hubs and the interactions between them.

These are my conclusions coming from the field. Perhaps you can twit @dfowler about this issue and see if it has (many) more authoritative recommendations.

+2
source

Source: https://habr.com/ru/post/1433956/


All Articles