public async Task <MazeSocket> AcceptAsync() { var key = string.Join(", ", _context.Request.Headers[MazeSocketHeaders.SecWebSocketKey]); var responseHeaders = HandshakeHelpers.GenerateResponseHeaders(key); foreach (var headerPair in responseHeaders) { _context.Response.Headers[headerPair.Key] = headerPair.Value; } var stream = await _upgradeFeature.UpgradeAsync(); await Task.Delay(500); string result; using (var streamReader = new StreamReader(stream)) { result = await streamReader.ReadLineAsync(); } Debug.Print("Result Server: " + result); using (var streamWriter = new StreamWriter(stream)) { await streamWriter.WriteLineAsync("Hey other"); } return(new MazeSocket(stream, _options.KeepAliveInterval)); }
public async Task <WebSocket> AcceptAsync(GameSocketAcceptContext acceptContext) { if (!IsGameSocketRequest) { throw new InvalidOperationException("Not a WebSocket request."); // TODO: LOC } var subProtocol = acceptContext?.SubProtocol; var keepAliveInterval = _options.KeepAliveInterval; var receiveBufferSize = _options.ReceiveBufferSize; if (acceptContext is ExtendedGameSocketAcceptContext advancedAcceptContext) { if (advancedAcceptContext.ReceiveBufferSize.HasValue) { receiveBufferSize = advancedAcceptContext.ReceiveBufferSize.Value; } if (advancedAcceptContext.KeepAliveInterval.HasValue) { keepAliveInterval = advancedAcceptContext.KeepAliveInterval.Value; } } var key = string.Join(", ", _context.Request.Headers[Constants.Headers.SecGameSocketKey]); HandshakeHelpers.GenerateResponseHeaders(key, subProtocol, _context.Response.Headers); var opaqueTransport = await _upgradeFeature.UpgradeAsync(); // Sets status code to 101 return(WebSocket.CreateFromStream(opaqueTransport, isServer: true, subProtocol: subProtocol, keepAliveInterval: keepAliveInterval)); }
public async Task <WebSocket> AcceptAsync(IWebSocketAcceptContext 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 WebSocketAcceptContext; 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)); }
private static async Task <WebSocket> Upgrade(HttpContext context) { var upgradeFeature = context.Features.Get <IHttpUpgradeFeature>(); // Generate WebSocket response headers string key = context.Request.Headers[Constants.Headers.SecWebSocketKey].ToString(); var responseHeaders = HandshakeHelpers.GenerateResponseHeaders(key); foreach (var headerPair in responseHeaders) { context.Response.Headers[headerPair.Key] = headerPair.Value; } // Upgrade the connection Stream opaqueTransport = await upgradeFeature.UpgradeAsync(); // Get the WebSocket object var ws = WebSocket.CreateFromStream(opaqueTransport, isServer: true, subProtocol: null, keepAliveInterval: TimeSpan.FromMinutes(2)); return(ws); }
public async Task <WebSocket> AcceptAsync(WebSocketAcceptContext acceptContext) { if (!IsWebSocketRequest) { throw new InvalidOperationException("Not a WebSocket request."); } string? subProtocol = null; bool enableCompression = false; bool serverContextTakeover = true; int serverMaxWindowBits = 15; TimeSpan keepAliveInterval = _options.KeepAliveInterval; if (acceptContext != null) { subProtocol = acceptContext.SubProtocol; enableCompression = acceptContext.DangerousEnableCompression; serverContextTakeover = !acceptContext.DisableServerContextTakeover; serverMaxWindowBits = acceptContext.ServerMaxWindowBits; keepAliveInterval = acceptContext.KeepAliveInterval ?? keepAliveInterval; } #pragma warning disable CS0618 // Type or member is obsolete if (acceptContext is ExtendedWebSocketAcceptContext advancedAcceptContext) #pragma warning restore CS0618 // Type or member is obsolete { if (advancedAcceptContext.KeepAliveInterval.HasValue) { keepAliveInterval = advancedAcceptContext.KeepAliveInterval.Value; } } HandshakeHelpers.GenerateResponseHeaders(!_isH2WebSocket, _context.Request.Headers, subProtocol, _context.Response.Headers); WebSocketDeflateOptions?deflateOptions = null; if (enableCompression) { var ext = _context.Request.Headers.SecWebSocketExtensions; if (ext.Count != 0) { // loop over each extension offer, extensions can have multiple offers, we can accept any foreach (var extension in _context.Request.Headers.GetCommaSeparatedValues(HeaderNames.SecWebSocketExtensions)) { if (extension.AsSpan().TrimStart().StartsWith("permessage-deflate", StringComparison.Ordinal)) { if (HandshakeHelpers.ParseDeflateOptions(extension.AsSpan().TrimStart(), serverContextTakeover, serverMaxWindowBits, out var parsedOptions, out var response)) { Log.CompressionAccepted(_logger, response); deflateOptions = parsedOptions; // If more extension types are added, this would need to be a header append // and we wouldn't want to break out of the loop _context.Response.Headers.SecWebSocketExtensions = response; break; } } } if (deflateOptions is null) { Log.CompressionNotAccepted(_logger); } } } Stream opaqueTransport; // HTTP/2 if (_isH2WebSocket) { // Send the response headers opaqueTransport = await _connectFeature !.AcceptAsync(); } // HTTP/1.1 else { opaqueTransport = await _upgradeFeature !.UpgradeAsync(); // Sets status code to 101 } return(WebSocket.CreateFromStream(opaqueTransport, new WebSocketCreationOptions() { IsServer = true, KeepAliveInterval = keepAliveInterval, SubProtocol = subProtocol, DangerousDeflateOptions = deflateOptions })); }