Пример #1
0
        public static IWebSocketHandler Create(WebSocketHttpRequest request, Action <string> onMessage, Action onClose, Action <byte[]> onBinary)
        {
            var readState = new ReadState();

            return(new ComposableHandler
            {
                Handshake = sub => Hybi13Handler.BuildHandshake(request, sub),
                TextFrame = s => Hybi13Handler.FrameData(Encoding.UTF8.GetBytes(s), FrameType.Text),
                BinaryFrame = s => Hybi13Handler.FrameData(s, FrameType.Binary),
                CloseFrame = i => Hybi13Handler.FrameData(i.ToBigEndianBytes <ushort>(), FrameType.Close),
                ReceiveData = d => Hybi13Handler.ReceiveData(d, readState, (op, data) => Hybi13Handler.ProcessFrame(op, data, onMessage, onClose, onBinary))
            });
        }
        public static IWebSocketHandler BuildHandler(WebSocketHttpRequest request, Action <string> onMessage, Action onClose, Action <byte[]> onBinary)
        {
            var version = GetVersion(request);

            switch (version)
            {
            case "76":
                return(Draft76Handler.Create(request, onMessage));

            case "7":
            case "8":
            case "13":
                return(Hybi13Handler.Create(request, onMessage, onClose, onBinary));
            }

            throw new WebSocketException(WebSocketStatusCodes.UnsupportedDataType);
        }