private async Task<WebSocket> AcceptWebSocketAsync(WebSocketAcceptContext 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; }
public async Task<WebSocket> AcceptAsync(WebSocketAcceptContext acceptContext) { if (!IsWebSocketRequest) { throw new InvalidOperationException("Not a WebSocket request."); // TODO: LOC } string subProtocol = null; if (acceptContext != null) { subProtocol = acceptContext.SubProtocol; } TimeSpan keepAliveInterval = _options.KeepAliveInterval; int receiveBufferSize = _options.ReceiveBufferSize; var advancedAcceptContext = acceptContext as ExtendedWebSocketAcceptContext; if (advancedAcceptContext != null) { if (advancedAcceptContext.ReceiveBufferSize.HasValue) { receiveBufferSize = advancedAcceptContext.ReceiveBufferSize.Value; } if (advancedAcceptContext.KeepAliveInterval.HasValue) { keepAliveInterval = advancedAcceptContext.KeepAliveInterval.Value; } } string key = string.Join(", ", _context.Request.Headers[Constants.Headers.SecWebSocketKey]); var responseHeaders = HandshakeHelpers.GenerateResponseHeaders(key, subProtocol); foreach (var headerPair in responseHeaders) { _context.Response.Headers[headerPair.Key] = headerPair.Value; } Stream opaqueTransport = await _upgradeFeature.UpgradeAsync(); // Sets status code to 101 return CommonWebSocket.CreateServerWebSocket(opaqueTransport, subProtocol, keepAliveInterval, receiveBufferSize); }
Task<WebSocket> IHttpWebSocketFeature.AcceptAsync(WebSocketAcceptContext context) { Context.HttpContext.Response.StatusCode = 101; // Switching Protocols var websockets = TestWebSocket.CreatePair(context.SubProtocol); _clientWebSocketTcs.SetResult(websockets.Item1); _serverWebSocket = websockets.Item2; return Task.FromResult<WebSocket>(_serverWebSocket); }
public Task<WebSocket> AcceptAsync(WebSocketAcceptContext context) { throw new NotImplementedException(); }