DotNetty.Codecs.Http.WebSockets.WebSocketServerHandshakerFactory.NewHandshaker C# (CSharp) Method

NewHandshaker() public method

Instances a new handshaker
public NewHandshaker ( IHttpRequest req ) : DotNetty.Codecs.Http.WebSockets.WebSocketServerHandshaker
req IHttpRequest
return DotNetty.Codecs.Http.WebSockets.WebSocketServerHandshaker
        public WebSocketServerHandshaker NewHandshaker(IHttpRequest req)
        {
            if (req.Headers.TryGet(HttpHeaderNames.SecWebsocketVersion, out ICharSequence version)
                && version is object)
            {
                if (version.Equals(WebSocketVersion.V13.ToHttpHeaderValue()))
                {
                    // Version 13 of the wire protocol - RFC 6455 (version 17 of the draft hybi specification).
                    return new WebSocketServerHandshaker13(_webSocketUrl, _subprotocols, _decoderConfig);
                }
                else if (version.Equals(WebSocketVersion.V08.ToHttpHeaderValue()))
                {
                    // Version 8 of the wire protocol - version 10 of the draft hybi specification.
                    return new WebSocketServerHandshaker08(_webSocketUrl, _subprotocols, _decoderConfig);
                }
                else if (version.Equals(WebSocketVersion.V07.ToHttpHeaderValue()))
                {
                    // Version 8 of the wire protocol - version 07 of the draft hybi specification.
                    return new WebSocketServerHandshaker07(_webSocketUrl, _subprotocols, _decoderConfig);
                }
                else
                {
                    return null;
                }
            }
            else
            {
                // Assume version 00 where version header was not specified
                return new WebSocketServerHandshaker00(_webSocketUrl, _subprotocols, _decoderConfig);
            }
        }

Usage Example

        public override void ChannelRead(IChannelHandlerContext ctx, object msg)
        {
            var req = (IFullHttpRequest)msg;

            if (this.IsNotWebSocketPath(req))
            {
                ctx.FireChannelRead(msg);
                return;
            }

            try
            {
                if (!Equals(req.Method, Get))
                {
                    SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, Forbidden));
                    return;
                }

                var wsFactory = new WebSocketServerHandshakerFactory(
                    GetWebSocketLocation(ctx.Channel.Pipeline, req, this.websocketPath), this.subprotocols,
                    this.allowExtensions, this.maxFramePayloadSize, this.allowMaskMismatch);
                WebSocketServerHandshaker handshaker = wsFactory.NewHandshaker(req);
                if (handshaker == null)
                {
                    WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(ctx.Channel);
                }
                else
                {
                    Task task = handshaker.HandshakeAsync(ctx.Channel, req);
                    task.ContinueWith(t =>
                    {
                        if (t.Status != TaskStatus.RanToCompletion)
                        {
                            ctx.FireExceptionCaught(t.Exception);
                        }
                        else
                        {
                            ctx.FireUserEventTriggered(new WebSocketServerProtocolHandler.HandshakeComplete(
                                                           req.Uri, req.Headers, handshaker.SelectedSubprotocol));
                        }
                    },
                                      TaskContinuationOptions.ExecuteSynchronously);

                    WebSocketServerProtocolHandler.SetHandshaker(ctx.Channel, handshaker);
                    ctx.Channel.Pipeline.Replace(this, "WS403Responder",
                                                 WebSocketServerProtocolHandler.ForbiddenHttpRequestResponder());
                }
            }
            finally
            {
                req.Release();
            }
        }
All Usage Examples Of DotNetty.Codecs.Http.WebSockets.WebSocketServerHandshakerFactory::NewHandshaker