Exemplo n.º 1
0
        public WebSocketClient(
            Uri url,
            WebSocketMessageSender sender       = null,
            ReceiverMemoryConfig receiverConfig = null,
            ILogger logger                       = null,
            string name                          = "CLIENT",
            Encoding messageEncoding             = null,
            Func <ClientWebSocket> clientFactory = null)
        {
            if (!ValidationUtils.ValidateInput(url))
            {
                throw new ArgumentNullException(nameof(url));
            }

            Url             = url;
            Name            = name;
            _logger         = logger;
            MessageEncoding = messageEncoding ?? Encoding.UTF8;

            receiverConfig = receiverConfig ?? ReceiverMemoryConfig.Default; //cannot use =?? in unity
            _memoryPool    = new MemoryPool(receiverConfig.InitialMemorySize, receiverConfig.MarginSize, logger);

            _clientFactory = clientFactory ?? DefaultClientFactory;

            _webSocketMessageSender = sender?.AsCore() ?? new SingleQueueSenderCore();
            _webSocketMessageSender.SetConfig(MessageEncoding, logger, Name);

            _webSocketMessageSender
            .ExceptionHappenedInSending
            .Subscribe(x =>
            {
                _backgroundExceptionQueue.Enqueue(x);
                Dispose();
            });
        }
Exemplo n.º 2
0
        public WebSocketClient(
            WebSocket connectedSocket,
            WebSocketMessageSender sender       = null,
            ReceiverMemoryConfig receiverConfig = null,
            ILogger logger           = null,
            string name              = "CLIENT",
            Encoding messageEncoding = null)
        {
            _socket = connectedSocket ?? throw new ArgumentNullException(nameof(connectedSocket));

            Url            = null;
            _clientFactory = null;

            Name            = name;
            _logger         = logger;
            MessageEncoding = messageEncoding ?? Encoding.UTF8;

            receiverConfig = receiverConfig ?? ReceiverMemoryConfig.Default;
            _memoryPool    = new MemoryPool(receiverConfig.InitialMemorySize, receiverConfig.MarginSize, logger);

            _webSocketMessageSender = sender?.AsCore() ?? new SingleQueueSenderCore();
            _webSocketMessageSender.SetConfig(MessageEncoding, logger, Name);

            _webSocketMessageSender
            .ExceptionHappenedInSending
            .Subscribe(x =>
            {
                _backgroundExceptionQueue.Enqueue(x);
                Dispose();
            });
        }