public override Task <WebSocket> AcceptWebSocketAsync(WebSocketAcceptContext acceptContext) { if (WebSocketFeature == null) { throw new NotSupportedException("WebSockets are not supported"); } return(WebSocketFeature.AcceptAsync(acceptContext)); }
public async Task <WebSocket> ConnectAsync(Uri uri, CancellationToken cancellationToken) { WebSocketFeature webSocketFeature = null; var contextBuilder = new HttpContextBuilder(_application, AllowSynchronousIO, PreserveExecutionContext); contextBuilder.Configure(context => { var request = context.Request; var scheme = uri.Scheme; scheme = (scheme == "ws") ? "http" : scheme; scheme = (scheme == "wss") ? "https" : scheme; request.Scheme = scheme; if (!request.Host.HasValue) { request.Host = uri.IsDefaultPort ? new HostString(HostString.FromUriComponent(uri).Host) : HostString.FromUriComponent(uri); } request.Path = PathString.FromUriComponent(uri); request.PathBase = PathString.Empty; if (request.Path.StartsWithSegments(_pathBase, out var remainder)) { request.Path = remainder; request.PathBase = _pathBase; } request.QueryString = QueryString.FromUriComponent(uri); request.Headers.Add(HeaderNames.Connection, new string[] { "Upgrade" }); request.Headers.Add(HeaderNames.Upgrade, new string[] { "websocket" }); request.Headers.Add(HeaderNames.SecWebSocketVersion, new string[] { "13" }); request.Headers.Add(HeaderNames.SecWebSocketKey, new string[] { CreateRequestKey() }); if (SubProtocols.Any()) { request.Headers.Add(HeaderNames.SecWebSocketProtocol, SubProtocols.ToArray()); } request.Body = Stream.Null; // WebSocket webSocketFeature = new WebSocketFeature(context); context.Features.Set <IHttpWebSocketFeature>(webSocketFeature); ConfigureRequest?.Invoke(context.Request); }); var httpContext = await contextBuilder.SendAsync(cancellationToken); if (httpContext.Response.StatusCode != StatusCodes.Status101SwitchingProtocols) { throw new InvalidOperationException("Incomplete handshake, status code: " + httpContext.Response.StatusCode); } if (webSocketFeature.ClientWebSocket == null) { throw new InvalidOperationException("Incomplete handshake"); } return(webSocketFeature.ClientWebSocket); }
public override Task <WebSocket> AcceptWebSocketAsync(string subProtocol) { if (WebSocketFeature == null) { throw new NotSupportedException("WebSockets are not supported"); } return(WebSocketFeature.AcceptAsync(new WebSocketAcceptContext() { SubProtocol = subProtocol })); }
public async Task <WebSocket> ConnectAsync(Uri uri, CancellationToken cancellationToken) { WebSocketFeature webSocketFeature = null; var contextBuilder = new HttpContextBuilder(_application); contextBuilder.Configure(context => { var request = context.Request; var scheme = uri.Scheme; scheme = (scheme == "ws") ? "http" : scheme; scheme = (scheme == "wss") ? "https" : scheme; request.Scheme = scheme; request.Path = PathString.FromUriComponent(uri); request.PathBase = PathString.Empty; if (request.Path.StartsWithSegments(_pathBase, out var remainder)) { request.Path = remainder; request.PathBase = _pathBase; } request.QueryString = QueryString.FromUriComponent(uri); request.Headers.Add("Connection", new string[] { "Upgrade" }); request.Headers.Add("Upgrade", new string[] { "websocket" }); request.Headers.Add("Sec-WebSocket-Version", new string[] { "13" }); request.Headers.Add("Sec-WebSocket-Key", new string[] { CreateRequestKey() }); request.Body = Stream.Null; // WebSocket webSocketFeature = new WebSocketFeature(context); context.Features.Set <IHttpWebSocketFeature>(webSocketFeature); ConfigureRequest?.Invoke(context.Request); }); var httpContext = await contextBuilder.SendAsync(cancellationToken); if (httpContext.Response.StatusCode != StatusCodes.Status101SwitchingProtocols) { throw new InvalidOperationException("Incomplete handshake, status code: " + httpContext.Response.StatusCode); } if (webSocketFeature.ClientWebSocket == null) { throw new InvalidOperationException("Incomplete handshake"); } return(webSocketFeature.ClientWebSocket); }