public void CloseConnection(WebSocketReceiverContext context) { bool keepAlive = false; if (context.WebSocket.CloseStatus == WebSocketCloseStatus.EndpointUnavailable) { keepAlive = true; } CloseConnection(context.ConnectionId, keepAlive); }
public static IWebSocketCommandInvocator GetInvocator(this WebSocketReceiverContext context, IServiceProvider serviceProvider) { if (context.InvocatorContext != null) { var instance = serviceProvider.GetService(context.InvocatorContext.Invocator); if (instance != null && instance is IWebSocketCommandInvocator) { return((IWebSocketCommandInvocator)instance); } } return(serviceProvider.GetService <IServerWebSocketCommandInvocator>()); }
public async Task ConnectAsync(WebSocket webSocket, string connectionId, string connectorName = "") { var receiverContext = new WebSocketReceiverContext { Compressor = _compressor, ConnectionId = connectionId, LoggerFactory = _loggerFactory, WebSocket = webSocket }; WebSocketTransport transport = null; if (Connections.TryGetValue(connectionId, out transport)) { transport.ReConnect(webSocket); List <MessageHolder> messages = _lifetimeManager.TryDequeue(connectionId); foreach (var message in messages) { await SendAsync(transport, new WebSocketMessageDescriptor { MessageType = WebSocketMessageType.Text, Segments = message.Segments, EndOfMessage = true, IsQueue = true, }); } } else { transport = new WebSocketTransport(webSocket, connectionId, connectorName); var context = new WebSocketMessageContext(); context.Command = WebSocketCommands.Handshake; context.Value = connectionId; context.Header = await _initState.GetStateAsync(); Connections.TryAdd(connectionId, transport); await SendAsync(connectionId, context); } var receiver = new WebSocketReceiver(_serviceProvider, receiverContext, CloseConnection); await receiver.ReceiveAsync(); }
public async Task ConnectAsync(WebSocket webSocket, string connectionId, string connectorName = "", CancellationToken cancellationToken = default(CancellationToken)) { if (cancellationToken != CancellationToken.None) { cancellationToken.Register(() => { CancellationGraceful(); }); } var receiverContext = new WebSocketReceiverContext { Compressor = _compressor, ConnectionId = connectionId, LoggerFactory = _loggerFactory, WebSocket = webSocket, InvocatorContext = InvocatorContext }; if (Connections.TryGetValue(connectionId, out WebSocketTransport transport)) { transport.ReConnect(webSocket); } else { transport = new WebSocketTransport(webSocket, connectionId, connectorName); var context = new WebSocketMessageContext(); context.Command = WebSocketCommands.Handshake; context.Value = connectionId; context.Header = await _initState.GetStateAsync(); Connections.TryAdd(connectionId, transport); await SendAsync(connectionId, context); } var receiver = new WebSocketReceiver(_serviceProvider, receiverContext, CloseConnection, _loggerFactory); await receiver.ReceiveAsync(cancellationToken); }
public void CloseConnection(WebSocketReceiverContext context) { CloseConnection(context.ConnectionId); }