-
Notifications
You must be signed in to change notification settings - Fork 37
Implement constraint handlers
As said earlier you define a base class to tell the library which events should be forwarded to the clients. note: In dotnet core version of library you no longer need to define base class for events, you instead implement IEventTypeFinder and lists which event you want to proxy.
But this is most often not enough, if a client will receive all events fired they will probably be slowed down to a very painful experience. Also there is a security issue with letting all users see all events.
For this the library has a constraint pattern.
To create a constraint, Implement
SignalR.EventAggregatorProxy.EventAggregation.IEventConstraintHandler<TEvent>
Or inherit the abstract base class
SignalR.EventAggregatorProxy.EventAggregation.EventConstraintHandler<TEvent>
It has one method that you need to implement, Allow
bool Allow(TEvent message, ConstraintContext context, dynamic constraint);
- First argument is the actual message with its data.
- Second argument is the is a Context object containing username and connectionId of the user that will receive the event.
- Third argument is a optional dynamic object, its defined on the client and contains data that you can use to constraint the event. See here how to use it client side.
Note Signature of Allow method was changed in 1.4.135, before it we used a string username instead of ConstraintContext
Return true if you want to forward this specific event to said user.
From version 1.1 you no longer need to use dynamic typed constraints. You can use static typed constraints. Inherit the abstract base class
SignalR.EventAggregatorProxy.EventAggregation.EventConstraintHandler<TEvent, TConstraint>
Dot net core does not support unregistered concrete types. So you need to register all your constraint handlers
services
.AddSignalREventAggregator()
.AddTransient<ConstrainedEventConstraintHandler>()