예제 #1
0
        /// <summary>
        /// 触发一个消息发送完成事件,置状态失败
        /// </summary>
        /// <param name="args"></param>
        /// <param name="context"></param>
        internal protected void OnWebSocketSendCompleted(HttpServerWebSocketContext context, WebSocketSendEventArgs args)
        {
            var action = this.WebSocketSendCompleted;

            if (action != null)
            {
                action(context, args);
            }
        }
예제 #2
0
        /// <summary>
        /// 引发连接断开事件
        /// </summary>
        /// <param name="context"></param>
        internal protected void OnWebSocketDisconnected(HttpServerWebSocketContext context)
        {
            lock (this.websockets)
                this.websockets.Remove(context.Id);

            if (this.WebSocketDisconnected != null)
            {
                this.WebSocketDisconnected(context);
            }
        }
예제 #3
0
        /// <summary>
        /// 触发一个消息
        /// </summary>
        /// <param name="data"></param>
        /// <param name="opcode"></param>
        /// <param name="context"></param>
        internal protected void OnWebSocketNewMessage(HttpServerWebSocketContext context, byte[] data, WebSocketOpcode opcode)
        {
            var action = this.WebSocketNewMessage;

            if (action != null)
            {
                var e = new WebSocketMessageEventArgs(opcode);
                e.Buffer = data;
                if (opcode == WebSocketOpcode.Text)
                {
                    e.Message = this.Encoding.GetString(data);
                }
                action(context, e);
            }
        }
예제 #4
0
        /// <summary>
        /// 遍历当前具有的WebSocket连接
        /// </summary>
        /// <param name="action"></param>
        public void WebSocketForEach(Action <HttpServerWebSocketContext> action)
        {
            HttpServerWebSocketContext[] contexts;
            lock (this.websockets)
            {
                contexts = new HttpServerWebSocketContext[this.websockets.Count];
                this.websockets.Values.CopyTo(contexts, 0);
            }

            //执行
            foreach (var context in contexts)
            {
                action(context);
            }
        }
예제 #5
0
        private bool WebSocketProcess(HttpServerWebSocketContext context)
        {
            var secKey = context.RequestHeader["Sec-WebSocket-Key"];

            if (string.IsNullOrEmpty(secKey))
            {
                //请求的版本不接受支持,最低版本:  7
                context.Response(HttpStatusCode.HttpVersionNotSupported);
                return(false);
            }

            //握手信息
            var acceptKey = WebSocketHandshake.HandshakeSecurityHash09(secKey);

            context.ResponseHeader.Add("Sec-WebSocket-Accept", acceptKey);

            //响应客户端连接
            context.Response(HttpStatusCode.SwitchingProtocols);

            //call event
            if (this.WebSocketConnectioned == null)
            {
                return(false);
            }

            this.WebSocketConnectioned(context);

            if (context.Allowed == false)
            {
                return(false);
            }

            //receive
            context.Receive();

            //add session
            lock (this.websockets)
            {
                this.websockets.Add(context.Id, context);
            }

            return(true);
        }
예제 #6
0
        private HttpServerContextBase ParseRequest(Socket socket)
        {
            SocketReader headReader = new SocketReader(socket, this.encoding, this.maxRequestHeadLength);
            var          line       = headReader.ReadStringLine();
            var          arr        = line.Split(' ');

            if (arr.Length != 3)
            {
                return(null);
            }
            var method   = arr[0];
            var url      = arr[1];
            var protocol = arr[2];

            var header = new NameValueCollection(10, StringComparer.OrdinalIgnoreCase);

            while ((line = headReader.ReadStringLine()) != null && !line.Equals(string.Empty))
            {
                var index = line.IndexOf(':');
                header[line.Substring(0, index)] = line.Substring(index + 1).Trim();
            }

            HttpServerContextBase context;

            if ("WebSocket".Equals(header["Upgrade"], StringComparison.OrdinalIgnoreCase))
            {
                context = new HttpServerWebSocketContext(socket, header, this, url, method, protocol);
            }
            else
            {
                context = new HttpServerContext(socket, header, this, url, method, protocol, this.maxRequestContentLength, this.fileHandler);
            }

            //querystring
            string path;

            UriHelper.ParseQueryString(context.Url, this.encoding, context.QueryString, out path);
            //
            return(context);
        }