private void DoWebSocketUpgrade(IDictionary<string, object> acceptOptions, WebSocketFunc callback) { if (callback == null) { throw new ArgumentNullException("callback"); } if (!TryStartResponse()) { throw new InvalidOperationException(Resources.Exception_ResponseAlreadySent); } _environment[Constants.ResponseStatusCodeKey] = 101; _acceptOptions = acceptOptions; _webSocketFunc = callback; // Call before PrepareReponse because it may do header cleanup. string subProtocol = GetWebSocketSubProtocol(); int receiveBufferSize = GetWebSocketReceiveBufferSize(); TimeSpan keepAliveInterval = GetWebSocketKeepAliveInterval(); ArraySegment<byte>? internalBuffer = GetWebSocketBuffer(); PrepareResponse(mayHaveBody: false); _webSocketAction = WebSocketUpgrade(subProtocol, receiveBufferSize, keepAliveInterval, internalBuffer); }
private void DoWebSocketUpgrade(IDictionary <string, object> acceptOptions, WebSocketFunc callback) { if (callback == null) { throw new ArgumentNullException("callback"); } if (!TryStartResponse()) { throw new InvalidOperationException(Resources.Exception_ResponseAlreadySent); } _environment[Constants.ResponseStatusCodeKey] = 101; _acceptOptions = acceptOptions; _webSocketFunc = callback; // Call before PrepareReponse because it may do header cleanup. string subProtocol = GetWebSocketSubProtocol(); int receiveBufferSize = GetWebSocketReceiveBufferSize(); TimeSpan keepAliveInterval = GetWebSocketKeepAliveInterval(); ArraySegment <byte>?internalBuffer = GetWebSocketBuffer(); PrepareResponse(mayHaveBody: false); _webSocketAction = WebSocketUpgrade(subProtocol, receiveBufferSize, keepAliveInterval, internalBuffer); }
private void DoWebSocketUpgrade(IDictionary<string, object> acceptOptions, WebSocketFunc callback) { if (callback == null) { throw new ArgumentNullException("callback"); } _environment[Constants.ResponseStatusCodeKey] = 101; _acceptOptions = acceptOptions; _webSocketFunc = callback; string subProtocol = GetWebSocketSubProtocol(); PrepareResponse(); // TODO: Other parameters? _webSocketAction = _context.AcceptWebSocketAsync(subProtocol) .Then(webSocketContext => { var wrapper = new WebSockets.OwinWebSocketWrapper(webSocketContext, _environment.Get<CancellationToken>(Constants.CallCancelledKey)); return _webSocketFunc(wrapper.Environment) .Then(() => wrapper.CleanupAsync()); }); }
private void DoWebSocketUpgrade(IDictionary <string, object> acceptOptions, WebSocketFunc callback) { if (callback == null) { throw new ArgumentNullException("callback"); } _environment[Constants.ResponseStatusCodeKey] = 101; _acceptOptions = acceptOptions; _webSocketFunc = callback; string subProtocol = GetWebSocketSubProtocol(); PrepareResponse(); // TODO: Other parameters? _webSocketAction = _context.AcceptWebSocketAsync(subProtocol) .Then(webSocketContext => { var wrapper = new WebSockets.OwinWebSocketWrapper(webSocketContext, _environment.Get <CancellationToken>(Constants.CallCancelledKey)); return(_webSocketFunc(wrapper.Environment) .Then(() => wrapper.CleanupAsync())); }); }
public static AppFunc Middleware(AppFunc app) { return(env => { var request = new Request(env); string opaqueSupport = request.Get <string>("opaque.Support"); OpaqueUpgrade opaqueUpgrade = request.Get <OpaqueUpgrade>("opaque.Upgrade"); string websocketSupport = request.Get <string>("websocket.Support"); WebSocketAccept webSocketAccept = request.Get <WebSocketAccept>("websocket.Accept"); if (opaqueSupport == "opaque.Upgrade" && // If we have opaque support opaqueUpgrade != null && websocketSupport != "websocket.Accept" && // and no current websocket support webSocketAccept == null) // and this request is a websocket request... { // This middleware is adding support for websockets. env["websocket.Support"] = "websocket.Accept"; if (IsWebSocketRequest(env)) { IDictionary <string, object> acceptOptions = null; WebSocketFunc webSocketFunc = null; // Announce websocket support env["websocket.Accept"] = new WebSocketAccept( (options, callback) => { acceptOptions = options; webSocketFunc = callback; env[OwinConstants.ResponseStatusCode] = 101; }); return app(env).Then(() => { Response response = new Response(env); if (response.StatusCode == 101 && webSocketFunc != null) { SetWebSocketResponseHeaders(env, acceptOptions); opaqueUpgrade(acceptOptions, opaqueEnv => { WebSocketLayer webSocket = new WebSocketLayer(opaqueEnv); return webSocketFunc(webSocket.Environment) .Then(() => webSocket.CleanupAsync()); }); } }); } } // else return app(env); }); }
public static AppFunc Middleware(AppFunc app) { return env => { var request = new OwinRequest(env); var websocketVersion = request.Get<string>(OwinConstants.WebSocket.Version); WebSocketAccept webSocketAccept = request.Get<WebSocketAccept>(OwinConstants.WebSocket.Accept); if (request.CanUpgrade && websocketVersion == null) // If we have opaque support and no WebSocket support yet { if (IsWebSocketRequest(env)) { // TODO: check for correct requested version of WebSocket protocol IDictionary<string, object> acceptOptions = null; WebSocketFunc webSocketFunc = null; // Announce websocket support env[OwinConstants.WebSocket.Accept] = new WebSocketAccept( (options, callback) => { acceptOptions = options; webSocketFunc = callback; env[OwinConstants.ResponseStatusCode] = 101; }); return app(env).ContinueWith(t => { OwinResponse response = new OwinResponse(env); if (response.StatusCode == 101 && webSocketFunc != null) { SetWebSocketResponseHeaders(env, acceptOptions); request.UpgradeDelegate(acceptOptions, opaqueEnv => { WebSocketLayer webSocket = new WebSocketLayer(opaqueEnv); return webSocketFunc(webSocket.Environment) .ContinueWith(tt => webSocket.CleanupAsync()); }); } }); } } // else return app(env); }; }
private void DoWebSocketUpgrade(IDictionary<string, object> acceptOptions, WebSocketFunc webSocketFunc) { if (webSocketFunc == null) { throw new ArgumentNullException("webSocketFunc"); } _env.ResponseStatusCode = 101; _webSocketFunc = webSocketFunc; var options = new AspNetWebSocketOptions(); options.SubProtocol = GetWebSocketSubProtocol(_env, acceptOptions); OnStart(); _httpContext.AcceptWebSocketRequest(AcceptCallback, options); }
private void DoWebSocketUpgrade(IDictionary <string, object> acceptOptions, WebSocketFunc webSocketFunc) { if (webSocketFunc == null) { throw new ArgumentNullException("webSocketFunc"); } _env.ResponseStatusCode = 101; _webSocketFunc = webSocketFunc; var options = new AspNetWebSocketOptions(); options.SubProtocol = GetWebSocketSubProtocol(_env, acceptOptions); OnStart(); _httpContext.AcceptWebSocketRequest(AcceptCallback, options); }
public static AppFunc Middleware(AppFunc app) { return(env => { string opaqueSupport = env.Get <string>("opaque.Support"); string websocketSupport = env.Get <string>("websocket.Support"); if (opaqueSupport == "OpaqueStreamFunc" && websocketSupport != "WebSocketFunc" && IsWebSocketRequest(env)) { // Announce websocket support env["websocket.Support"] = "WebSocketFunc"; return app(env).Then(() => { Response response = new Response(env); if (response.StatusCode == 101 && env.Get <WebSocketFunc>("websocket.Func") != null) { SetWebSocketResponseHeaders(env); WebSocketFunc wsBody = env.Get <WebSocketFunc>("websocket.Func"); OpaqueStreamFunc opaqueBody = (incoming, outgoing) => { WebSocketLayer webSocket = new WebSocketLayer(incoming, outgoing); return wsBody(webSocket.SendAsync, webSocket.ReceiveAsync, webSocket.CloseAsync) .Then(() => webSocket.CleanupAsync()); }; env["opaque.Func"] = opaqueBody; } }); } else { return app(env); } }); }