void HandleHttpRequest(IChannelHandlerContext ctx, IFullHttpRequest req) { if (!req.Result.IsSuccess) { SendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.BadRequest)); return; } if (req.Uri.Equals(WebsocketPath)) { var wsFactory = new WebSocketServerHandshakerFactory( GetWebSocketLocation(req), null, true, _webSocketConfig.MaxMessageLength); _handShaker = wsFactory.NewHandshaker(req); if (_handShaker == null) { WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(ctx.Channel); } else { _handShaker.HandshakeAsync(ctx.Channel, req); } ConsoleHelper.ConDepServerWriteLine($"新的连接{ctx.Channel.Id}"); return; } if (req.Uri.Equals("/favicon.ico")) { var res = new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.NotFound); SendHttpResponse(ctx, req, res); return; } var httpHandler = new HttpHandler(); DefaultFullHttpResponse httpResponse = httpHandler.GetResponse(req); SendHttpResponse(ctx, req, httpResponse); }
void HandleHttpRequest(IChannelHandlerContext ctx, IFullHttpRequest req) { // Handle a bad request. if (!req.Result.IsSuccess) { SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, BadRequest)); return; } // Allow only GET methods. if (!Equals(req.Method, HttpMethod.Get)) { SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, Forbidden)); return; } // Handshake var wsFactory = new WebSocketServerHandshakerFactory( GetWebSocketLocation(req), null, true, 5 * 1024 * 1024); _handshaker = wsFactory.NewHandshaker(req); if (this._handshaker == null) { WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(ctx.Channel); } else { _logger.LogInformation($"New client to handshake:{ctx.Channel}"); var appSession = new AppSession(); appSession.Initialize(ctx.Channel, ctx.Channel.RemoteAddress); _appSessionContainer.Set(ctx.Channel.Id.ToString(), appSession); _handshaker.HandshakeAsync(ctx.Channel, req); } }
private void HandleHttpRequest(IChannelHandlerContext context, IFullHttpRequest request) { // Handle a bad request. if (!request.Result.IsSuccess) { SendHttpResponse(context, request, new DefaultFullHttpResponse(Http11, BadRequest)); return; } // Allow only GET methods. if (!Equals(request.Method, HttpMethod.Get)) { SendHttpResponse(context, request, new DefaultFullHttpResponse(Http11, Forbidden)); return; } // Send the demo page and favicon.ico if ("/".Equals(request.Uri, StringComparison.OrdinalIgnoreCase)) { var content = WebSocketServerBenchmarkPage.GetContent(GetWebSocketLocation(request)); var response = new DefaultFullHttpResponse(Http11, OK, content); response.Headers.Set(HttpHeaderNames.ContentType, "text/html; charset=UTF-8"); HttpUtil.SetContentLength(response, content.ReadableBytes); SendHttpResponse(context, request, response); return; } if ("/favicon.ico".Equals(request.Uri, StringComparison.OrdinalIgnoreCase)) { var res = new DefaultFullHttpResponse(Http11, NotFound); SendHttpResponse(context, request, res); return; } // Handshake var wsFactory = new WebSocketServerHandshakerFactory( GetWebSocketLocation(request), null, true, 5 * 1024 * 1024); handshaker = wsFactory.NewHandshaker(request); if (handshaker.IsNull()) { WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(context.Channel); } else { handshaker.HandshakeAsync(context.Channel, request); } }
void HandleHttpRequest(IChannelHandlerContext ctx, IFullHttpRequest req) { if (!req.Result.IsSuccess) { SendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.BadRequest)); return; } if (!Equals(req.Method, HttpMethod.Get)) { SendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.Forbidden)); return; } if ("/api".Equals(req.Uri)) { IByteBuffer content = WebSocketServerBenchmarkPage.GetContent(GetWebSocketLocation(req)); var res = new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.OK, content); res.Headers.Set(HttpHeaderNames.ContentType, "text/html; charset=UTF-8"); HttpUtil.SetContentLength(res, content.ReadableBytes); SendHttpResponse(ctx, req, res); return; } if ("/favicon.ico".Equals(req.Uri)) { var res = new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.NotFound); SendHttpResponse(ctx, req, res); return; } // Handshake var wsFactory = new WebSocketServerHandshakerFactory( GetWebSocketLocation(req), null, true, 5 * 1024 * 1024); _handShaker = wsFactory.NewHandshaker(req); if (_handShaker == null) { WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(ctx.Channel); } else { _handShaker.HandshakeAsync(ctx.Channel, req); } }
private void HandleHttpRequest(IChannelHandlerContext ctx, IFullHttpRequest req) { // Handle a bad request. if (!req.Result.IsSuccess) { Extention.SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, BadRequest)); return; } // Allow only GET methods. if (!Equals(req.Method, HttpMethod.Get)) { Extention.SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, Forbidden)); return; } if ("/favicon.ico".Equals(req.Uri)) { var res = new DefaultFullHttpResponse(Http11, NotFound); Extention.SendHttpResponse(ctx, req, res); return; } // Handshake var wsFactory = new WebSocketServerHandshakerFactory(Extention.GetWebSocketLocation(req, _server._path, _server._useSsl), null, true, 5 * 1024 * 1024); handshaker = wsFactory.NewHandshaker(req); if (handshaker == null) { WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(ctx.Channel); } else { handshaker.HandshakeAsync(ctx.Channel, req); } _server.connectionDict.TryGetValue(ctx.Channel.Id.AsShortText(), out WebSocketConnection conn); _server._event.OnConnectionConnectAction?.Invoke(conn); }