Fork me on GitHub

ASP.NET SignalR HubPipelineModule

ASP.NET SignalR 1.0 实现的一个特性HubPipeline -实现任何消息incoming和outgoing的拦截。SignalR HubPipeline功能对应的ASP.NET Web API和ASP.NET MVC的 ActionFilter。

下面的方法是作为一个实体在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);
}

是不是感觉有非常复杂的委托。不用急, HubPipelineModule 类已经为我们实现了大部分的功能,大部分情况下已经够用了,可以继承这个雷重写里面方法就可以了.

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);
}

这代码看起来是不是和ASP.NET Web API和ASP.NET MVC的 ActionFilter 很相似呢。如果其中称为OnBefore的方法返回false,它会立即结束。

 
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;
    }
}

记录下连接到每个ID所请求的时间,我们简单地检查下一个请求的时间间隔。  如果2个请求之间的时间差小于我们设定的时间间隔,直接返回。

您可以把它添加到使用GlobalHost的模块Global.asax.cs

protected void Application_Start(object sender, EventArgs e)
{
    GlobalHost.HubPipeline.AddModule(new AntiClickModule());
}
posted @ 2013-04-05 21:06 张善友 阅读(...) 评论(...) 编辑 收藏