示例#1
0
 public static async Task Accept(Type handlerType, WebSocketAcceptContext acceptContext)
 {
     using (var handler = handlerType.New <WebSocketHandler>())
     {
         await Accept(handler, acceptContext);
     }
 }
示例#2
0
        public async Task Start <T>(string uri, WebSocketBuildOption options) where T : WebSocketHandler, new()
        {
            var listener = new HttpListener();

            listener.Prefixes.Add(uri);
            listener.Start();

            while (true)
            {
                var listenerContext = await listener.GetContextAsync();

                if (listenerContext.Request.IsWebSocketRequest)
                {
                    var socketContext = await listenerContext.AcceptWebSocketAsync(null, options.KeepAliveInterval);

                    var acceptContext = new WebSocketAcceptContext(socketContext.WebSocket, listenerContext.User, options);
                    await WebSocketHandler.Accept <T>(acceptContext);
                }
                else
                {
                    listenerContext.Response.StatusCode = 404;
                    listenerContext.Response.Close();
                }
            }
        }
示例#3
0
        public async Task Start <T>(string uri, TimeSpan keepAliveInterval, TimeSpan heartbeatInterval, int heartbeatTryTimes = 3) where T : WebSocketHandler, new()
        {
            var listener = new HttpListener();

            listener.Prefixes.Add(uri);
            listener.Start();

            while (true)
            {
                var listenerContext = await listener.GetContextAsync();

                if (listenerContext.Request.IsWebSocketRequest)
                {
                    var socketContext = await listenerContext.AcceptWebSocketAsync(null, keepAliveInterval);

                    var acceptContext = new WebSocketAcceptContext(socketContext.WebSocket, listenerContext.User, heartbeatInterval, heartbeatTryTimes);
                    await WebSocketHandler.Accept <T>(acceptContext);
                }
                else
                {
                    listenerContext.Response.StatusCode = 404;
                    listenerContext.Response.Close();
                }
            }
        }
示例#4
0
        public async Task Invoke(HttpContext context)
        {
            if (context.WebSockets.IsWebSocketRequest)
            {
                var webSocket = await context.WebSockets.AcceptWebSocketAsync();

                var handlerType = _option.GetHandlerType(context.Request.Path);

                if (handlerType != null && typeof(WebSocketHandler).IsAssignableFrom(handlerType))
                {
                    var handler = GetHandler(handlerType);
                    if (handler != null)
                    {
                        using (var scope = context.RequestServices.CreateScope())
                        {
                            var acceptContext = new WebSocketAcceptContext(scope.ServiceProvider, webSocket, context.User, _option);
                            await WebSocketHandler.Accept(handler, acceptContext);
                        }
                    }
                }
            }
            else
            {
                await _next(context);
            }
        }
示例#5
0
        public async Task Invoke(HttpContext context)
        {
            if (context.WebSockets.IsWebSocketRequest)
            {
                var webSocket = await context.WebSockets.AcceptWebSocketAsync();

                var handlerType = option.GetHandlerType(context.Request.Path);

                if (handlerType == null || !typeof(WebSocketHandler).IsAssignableFrom(handlerType))
                {
                    context.Response.StatusCode = 400;
                }
                else
                {
                    var handler = GetHandler(handlerType);
                    if (handler != null)
                    {
                        var acceptContext = new WebSocketAcceptContext(webSocket, context.User, option);
                        await WebSocketHandler.Accept(handler, acceptContext);
                    }
                }
            }
            else
            {
                await next(context);
            }
        }
示例#6
0
        public override void Initialize(WebSocketAcceptContext acceptContext)
        {
            base.Initialize(acceptContext);

            _aliveKey = acceptContext.Option.AliveKey;

            //开启消息订阅,使用aliveKey作为通道
            _subscribeMgr = acceptContext.ServiceProvider.TryGetService <ISubscribeManager>();
            _cacheMgr     = acceptContext.ServiceProvider.TryGetService <IDistributedCacheManager>();

            if (_cacheMgr == null)
            {
                throw new NotSupportedException("必须使用分布式缓存组件。");
            }

            _subscribeMgr.AddSubscriber <DistributedInvokeMessage>(_aliveKey, msg =>
            {
                try
                {
                    //收到消息后,在本地查找连接,并发送消息
                    Clients(msg.Connections.ToArray()).SendAsync(msg.Message.Method, msg.Message.Arguments);
                }
                catch { }
            });
        }
示例#7
0
        public static async Task Accept(WebSocketHandler handler, WebSocketAcceptContext acceptContext)
        {
            handler.acceptContext = acceptContext;
            handler.Clients       = ClientManager.GetManager(handler.GetType(), acceptContext.Option);

            await handler.Invoke();
        }
示例#8
0
 internal static IClientManager GetManager(Type handlerType, WebSocketAcceptContext acceptContext)
 {
     return(_managers.GetOrAdd(handlerType, () =>
     {
         var manager = acceptContext.ServiceProvider.TryGetService <IClientManager>(() => new DefaultClientManager());
         manager.Initialize(acceptContext);
         return manager;
     }));
 }
示例#9
0
        public virtual void Initialize(WebSocketAcceptContext acceptContext)
        {
            _option = acceptContext.Option;

            //开启一个定时器,一定时间去检查一下有没有死亡而没有释放的连接实例
            if (_option.HeartbeatInterval != TimeSpan.Zero)
            {
                _timer = new Timer(CheckAlive, null, TimeSpan.FromSeconds(10), _option.HeartbeatInterval);
            }
        }
示例#10
0
 void IHttpHandler.ProcessRequest(HttpContext context)
 {
     if (context.IsWebSocketRequest)
     {
         context.AcceptWebSocketRequest(async c =>
         {
             var handlerType = WebSocketBuildOption.Default.GetHandlerType(context.Request.Path);
             if (handlerType != null && typeof(WebSocketHandler).IsAssignableFrom(handlerType))
             {
                 var acceptContext = new WebSocketAcceptContext(c.WebSocket, context.User, WebSocketBuildOption.Default);
                 await WebSocketHandler.Accept(handlerType, acceptContext);
             }
         });
     }
 }
示例#11
0
        public static async Task Accept(WebSocketHandler handler, WebSocketAcceptContext acceptContext)
        {
            handler.cancelToken   = new CancellationTokenSource();
            handler.acceptContext = acceptContext;
            handler.Clients       = ClientManager.GetManager(handler.GetType(), acceptContext.Option);

            try
            {
                await handler.InvokeAsync();
            }
            catch (Exception exp)
            {
                handler.OnFatalError(exp);
            }
        }
示例#12
0
 void IHttpHandler.ProcessRequest(HttpContext context)
 {
     if (context.IsWebSocketRequest)
     {
         context.AcceptWebSocketRequest(async c =>
         {
             var handlerType = WebSocketBuildOption.Default.GetHandlerType(context.Request.Path);
             if (handlerType != null && typeof(WebSocketHandler).IsAssignableFrom(handlerType))
             {
                 using (var scope = ContainerUnity.GetContainer().CreateScope())
                 {
                     var acceptContext = new WebSocketAcceptContext(scope.ServiceProvider, c.WebSocket, context.User, WebSocketBuildOption.Default);
                     await WebSocketHandler.Accept(handlerType, acceptContext);
                 }
             }
         });
     }
 }
示例#13
0
 void IHttpHandler.ProcessRequest(HttpContext context)
 {
     if (context.IsWebSocketRequest)
     {
         context.AcceptWebSocketRequest(async c =>
         {
             var handlerType = WebSocketBuildOption.Default.GetHandlerType(context.Request.Path);
             if (handlerType == null || !typeof(WebSocketHandler).IsAssignableFrom(handlerType))
             {
                 context.Response.StatusCode = 400;
             }
             else
             {
                 var acceptContext = new WebSocketAcceptContext(c.WebSocket, context.User, WebSocketBuildOption.Default.HeartbeatInterval, WebSocketBuildOption.Default.HeartbeatTryTimes);
                 await WebSocketHandler.Accept(handlerType, acceptContext);
             }
         });
     }
 }
示例#14
0
 public static async Task Accept <T>(WebSocketAcceptContext acceptContext) where T : WebSocketHandler, new()
 {
     await Accept(typeof(T), acceptContext);
 }
 public SubscribeLifetimeManager(WebSocketAcceptContext context)
 {
     this.context = context;
 }
示例#16
0
 public DefaultLifetimeManager(WebSocketAcceptContext context, ClientManager d)
 {
     this.context = context;
 }
示例#17
0
 public void Initialize(WebSocketAcceptContext context)
 {
     _innerMgr.Initialize(context);
 }