Beispiel #1
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();
        }
Beispiel #2
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);
        }