Exemplo n.º 1
0
        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>());
        }
Exemplo n.º 3
0
        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();
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
 public void CloseConnection(WebSocketReceiverContext context)
 {
     CloseConnection(context.ConnectionId);
 }