public async Task InvokeAsync(HttpContext context) { if (context.WebSockets.IsWebSocketRequest && context.IsValidPath(Options.SubscriptionPath)) { OnConnectWebSocketAsync onConnect = Options.OnConnectWebSocket ?? GetService <OnConnectWebSocketAsync>(context); OnCreateRequestAsync onRequest = Options.OnCreateRequest ?? GetService <OnCreateRequestAsync>(context); WebSocketSession session = await WebSocketSession .TryCreateAsync(context, Executer, onConnect, onRequest) .ConfigureAwait(false); if (session != null) { await session.StartAsync(context.RequestAborted) .ConfigureAwait(false); } } else { await _next(context).ConfigureAwait(false); } }
public static IServiceCollection AddWebSocketConnectionInterceptor( this IServiceCollection services, OnConnectWebSocketAsync interceptor) { return(services .AddSingleton <ISocketConnectionInterceptor <HttpContext> >( new SocketConnectionDelegateInterceptor(interceptor))); }
public SocketConnectionDelegateInterceptor( OnConnectWebSocketAsync interceptor) { if (interceptor is null) { throw new ArgumentNullException(nameof(interceptor)); } _interceptor = interceptor; }
public WebSocketContext( HttpContext httpContext, WebSocket webSocket, IQueryExecuter queryExecuter, OnConnectWebSocketAsync onConnectAsync, OnCreateRequestAsync onCreateRequest) { HttpContext = httpContext ?? throw new ArgumentNullException(nameof(httpContext)); WebSocket = webSocket ?? throw new ArgumentNullException(nameof(webSocket)); QueryExecuter = queryExecuter ?? throw new ArgumentNullException(nameof(queryExecuter)); _onConnectAsync = onConnectAsync; _onCreateRequest = onCreateRequest; }
public static async Task <WebSocketSession> TryCreateAsync( HttpContext httpContext, IQueryExecutor queryExecutor, OnConnectWebSocketAsync onConnectAsync, OnCreateRequestAsync onCreateRequest) { if (httpContext == null) { throw new ArgumentNullException(nameof(httpContext)); } if (queryExecutor == null) { throw new ArgumentNullException(nameof(queryExecutor)); } WebSocket socket = await httpContext.WebSockets .AcceptWebSocketAsync(_protocol) .ConfigureAwait(false); if (httpContext.WebSockets.WebSocketRequestedProtocols .Contains(socket.SubProtocol)) { var context = new WebSocketContext( new HttpContextWrapper(httpContext), new WebSocketWrapper(socket), queryExecutor, onConnectAsync, onCreateRequest); return(new WebSocketSession(context)); } else { await socket.CloseAsync( WebSocketCloseStatus.ProtocolError, "Expected graphql-ws protocol.", CancellationToken.None) .ConfigureAwait(false); socket.Dispose(); } return(null); }