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