ASP. NET signalr hubpipelinemodule

Source: Internet
Author: User

Hubpipeline, a feature implemented by ASP. NET signalr 1.0, is used to intercept incoming and outgoing messages. The ASP. NET web API corresponding to the signalr hubpipeline function and the actionfilter of ASP. net mvc.

The following method is defined as an object in ihubpipelinemodule:

public interface IHubPipelineModule{    Func<IHubIncomingInvokerContext, Task<object>> BuildIncoming(Func<IHubIncomingInvokerContext, Task<object>> invoke);    Func<IHubOutgoingInvokerContext, Task> BuildOutgoing(Func<IHubOutgoingInvokerContext, Task> send);    Func<IHub, Task> BuildConnect(Func<IHub, Task> connect);    Func<IHub, Task> BuildReconnect(Func<IHub, Task> reconnect);    Func<IHub, Task> BuildDisconnect(Func<IHub, Task> disconnect);    Func<HubDescriptor, IRequest, bool> BuildAuthorizeConnect(Func<HubDescriptor, IRequest, bool> authorizeConnect);    Func<HubDescriptor, IRequest, IList<string>, IList<string>> BuildRejoiningGroups(Func<HubDescriptor, IRequest, IList<string>, IList<string>> rejoiningGroups);}

Is there a complicated commission. Don't worry. The hubpipelinemodule class has already implemented most of the functions for us. In most cases, it is enough. You can inherit the method in this relay rewrite.

public abstract class HubPipelineModule : IHubPipelineModule{    protected virtual bool OnBeforeAuthorizeConnect(HubDescriptor hubDescriptor, IRequest request);    protected virtual bool OnBeforeConnect(IHub hub);    protected virtual void OnAfterConnect(IHub hub);    protected virtual bool OnBeforeReconnect(IHub hub);    protected virtual void OnAfterReconnect(IHub hub);    protected virtual bool OnBeforeOutgoing(IHubOutgoingInvokerContext context);    protected virtual void OnAfterOutgoing(IHubOutgoingInvokerContext context);    protected virtual bool OnBeforeDisconnect(IHub hub);    protected virtual void OnAfterDisconnect(IHub hub);    protected virtual bool OnBeforeIncoming(IHubIncomingInvokerContext context);    protected virtual object OnAfterIncoming(object result, IHubIncomingInvokerContext context);    protected virtual void OnIncomingError(Exception ex, IHubIncomingInvokerContext context);}

Does this code look similar to ASP. NET web API and ASP. net mvc actionfilter. If the method called onbefore returns false, it ends immediately.

 
public class AntiClickModule : HubPipelineModule{    public AntiClickModule()    {        Interval = 1000;    }    public int Interval { get; set; }    private readonly ConcurrentDictionary<string, DateTime> _connections = new ConcurrentDictionary<string, DateTime>();    protected override void OnAfterDisconnect(IHub hub)    {        DateTime lastDateTime;        _connections.TryRemove(hub.Context.ConnectionId, out lastDateTime);    }    protected override bool OnBeforeIncoming(IHubIncomingInvokerContext context)    {        var now = DateTime.Now;        var connectionId = context.Hub.Context.ConnectionId;        DateTime lastDateTime;        if (_connections.TryGetValue(connectionId, out lastDateTime))        {            var span = now - lastDateTime;            if (span.TotalMilliseconds < Interval)            {                return false;            }        }        _connections.AddOrUpdate(connectionId, now, (_, __) => now);        return true;    }}

Record the request time connected to each ID. We simply check the interval of the next request. If the time difference between two requests is less than the preset time interval, the system returns the result directly.

You can add it to the Global. asax. CS module that uses globalhost.

protected void Application_Start(object sender, EventArgs e){    GlobalHost.HubPipeline.AddModule(new AntiClickModule());}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.