private async Task <WebSocket> AcceptWebSocketAsync(IWebSocketAcceptContext context) { IDictionary <string, object> options = null; if (context is OwinWebSocketAcceptContext) { var acceptContext = context as OwinWebSocketAcceptContext; options = acceptContext.Options; _subProtocol = acceptContext.SubProtocol; } else if (context != null && context.SubProtocol != null) { options = new Dictionary <string, object>(1) { { OwinConstants.WebSocket.SubProtocol, context.SubProtocol } }; _subProtocol = context.SubProtocol; } // Accept may have been called synchronously on the original request thread, we might not have a task yet. Go async. await _upstreamWentAsync.Task; _owinWebSocketAccept(options, OwinAcceptCallback); _requestTcs.TrySetResult(0); // Let the pipeline unwind. return(await _acceptTcs.Task); }
private async Task<WebSocket> AcceptWebSocketAsync(IWebSocketAcceptContext context) { IDictionary<string, object> options = null; if (context is OwinWebSocketAcceptContext) { var acceptContext = context as OwinWebSocketAcceptContext; options = acceptContext.Options; _subProtocol = acceptContext.SubProtocol; } else if (context != null && context.SubProtocol != null) { options = new Dictionary<string, object>(1) { { OwinConstants.WebSocket.SubProtocol, context.SubProtocol } }; _subProtocol = context.SubProtocol; } // Accept may have been called synchronously on the original request thread, we might not have a task yet. Go async. await _upstreamWentAsync.Task; _owinWebSocketAccept(options, OwinAcceptCallback); _requestTcs.TrySetResult(0); // Let the pipeline unwind. return await _acceptTcs.Task; }
Task <WebSocket> IHttpWebSocketFeature.AcceptAsync(IWebSocketAcceptContext context) { object obj; if (!Environment.TryGetValue(OwinConstants.WebSocket.AcceptAlt, out obj)) { throw new NotSupportedException("WebSockets are not supported"); // TODO: LOC } var accept = (Func <IWebSocketAcceptContext, Task <WebSocket> >)obj; return(accept(context)); }
public static AppFunc AdaptWebSockets(AppFunc next) { return(async environment => { object accept; if (environment.TryGetValue(OwinConstants.WebSocket.AcceptAlt, out accept) && accept is WebSocketAcceptAlt) { var adapter = new WebSocketAcceptAdapter(environment, (WebSocketAcceptAlt)accept); environment[OwinConstants.WebSocket.Accept] = new WebSocketAccept(adapter.AcceptWebSocket); await next(environment); if ((int)environment[OwinConstants.ResponseStatusCode] == 101 && adapter._callback != null) { IWebSocketAcceptContext acceptContext = null; object obj; if (adapter._options != null && adapter._options.TryGetValue(typeof(IWebSocketAcceptContext).FullName, out obj)) { acceptContext = obj as IWebSocketAcceptContext; } else if (adapter._options != null) { acceptContext = new OwinWebSocketAcceptContext(adapter._options); } var webSocket = await adapter._accept(acceptContext); var webSocketAdapter = new WebSocketAdapter(webSocket, (CancellationToken)environment[OwinConstants.CallCancelled]); await adapter._callback(webSocketAdapter.Environment); await webSocketAdapter.CleanupAsync(); } } else { await next(environment); } }); }