예제 #1
0
 public WebSocketClient(IBootstrapWrapperFactory wrapperFactory,
                        ISigningCredentialsService signingCredentials,
                        DotNettyDependency dependency,
                        ILoggerFactory loggerFactory)
     : base(wrapperFactory, signingCredentials, dependency, loggerFactory)
 {
     _clientOptions = Options.WebSocketClient;
 }
예제 #2
0
        public async void Connect(Uri uri)
        {
            // not if already started
            if (webSocket != null)
            {
                // paul:  exceptions are better than silence
                ReceivedError?.Invoke(new Exception("Client already connected"));
                return;
            }

            this.uri = uri;
            // We are connecting from now until Connect succeeds or fails
            Connecting = true;

            var options = new WebSocketClientOptions()
            {
                NoDelay              = true,
                KeepAliveInterval    = TimeSpan.Zero,
                SecWebSocketProtocol = "binary"
            };

            cancellation = new CancellationTokenSource();

            var clientFactory = new WebSocketClientFactory();

            try
            {
                using (webSocket = await clientFactory.ConnectAsync(uri, options, cancellation.Token))
                {
                    var token = cancellation.Token;
                    IsConnected = true;
                    Connecting  = false;
                    Connected?.Invoke();

                    await ReceiveLoop(webSocket, token);
                }
            }
            catch (ObjectDisposedException)
            {
                // No error, the client got closed
            }
            catch (Exception ex)
            {
                ReceivedError?.Invoke(ex);
            }
            finally
            {
                Disconnect();
                Disconnected?.Invoke();
            }
        }
예제 #3
0
        public async Task Run()
        {
            // NOTE: if the service is so busy that it cannot respond to a PING within the KeepAliveInterval interval the websocket connection will be closed
            // To run extreme tests it is best to set the KeepAliveInterval to TimeSpan.Zero to disable ping pong
            WebSocketClientOptions options = new WebSocketClientOptions()
            {
                NoDelay = true, KeepAliveInterval = TimeSpan.FromSeconds(2), SecWebSocketProtocol = "chatV2, chatV1"
            };

            using (_webSocket = await _clientFactory.ConnectAsync(_uri, options))
            {
                var source = new CancellationTokenSource();
                _token = source.Token;
                Task recTask = Task.Run(ReceiveLoop);
                stopwatch = Stopwatch.StartNew();
                for (int i = 0; i < Constants.NoOfIterations; i++)
                {
                    var buffer = myTestBytes.Array.Clone() as byte[];
                    await _webSocket.SendAsync(new ArraySegment <byte>(buffer), WebSocketMessageType.Binary, true, source.Token);
                }
                recTask.Wait();

                /* Random rand = new Random(_seed);
                 * _expectedValues = new byte[50][];
                 * for (int i = 0; i < _expectedValues.Length; i++)
                 * {
                 *   int numBytes = rand.Next(_minNumBytesPerMessage, _maxNumBytesPerMessage);
                 *   byte[] bytes = new byte[numBytes];
                 *   rand.NextBytes(bytes);
                 *   _expectedValues[i] = bytes;
                 * }
                 *
                 * Task recTask = Task.Run(ReceiveLoop);
                 * byte[] sendBuffer = new byte[_maxNumBytesPerMessage];
                 * for (int i = 0; i < _numItems; i++)
                 * {
                 *   int index = i % _expectedValues.Length;
                 *   byte[] bytes = _expectedValues[index];
                 *   Buffer.BlockCopy(bytes, 0, sendBuffer, 0, bytes.Length);
                 *   ArraySegment<byte> buffer = new ArraySegment<byte>(sendBuffer, 0, bytes.Length);
                 *   await _webSocket.SendAsync(myPersonBytesMap[_maxNumBytesPerMessage], WebSocketMessageType.Binary, true, source.Token);
                 * }
                 *
                 * await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, null, source.Token);
                 * recTask.Wait();*/
            }
        }
예제 #4
0
        public async Task Run()
        {
            var factory = new WebSocketClientFactory();
            WebSocketClientOptions options = new WebSocketClientOptions()
            {
                NoDelay = true, KeepAliveInterval = TimeSpan.Zero
            };

            using (_webSocket = await factory.ConnectAsync(_uri, options))
            {
                var source = new CancellationTokenSource();
                _token = source.Token;

                Random rand = new Random(_seed);
                _expectedValues = new byte[50][];
                for (int i = 0; i < _expectedValues.Length; i++)
                {
                    int    numBytes = rand.Next(_minNumBytesPerMessage, _maxNumBytesPerMessage);
                    byte[] bytes    = new byte[numBytes];
                    rand.NextBytes(bytes);
                    _expectedValues[i] = bytes;
                }

                Task   recTask    = Task.Run(ReceiveLoop);
                byte[] sendBuffer = new byte[_maxNumBytesPerMessage];
                for (int i = 0; i < _numItems; i++)
                {
                    int    index = i % _expectedValues.Length;
                    byte[] bytes = _expectedValues[index];
                    Buffer.BlockCopy(bytes, 0, sendBuffer, 0, bytes.Length);
                    ArraySegment <byte> buffer = new ArraySegment <byte>(sendBuffer, 0, bytes.Length);
                    await _webSocket.SendAsync(buffer, WebSocketMessageType.Binary, true, source.Token);
                }

                await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "So long and thanks for all the bits", source.Token);

                recTask.Wait();
            }
        }
예제 #5
0
        public override async Task <IConnection> ConnectAsync(Uri uri)
        {
            var options = new WebSocketClientOptions
            {
                NoDelay              = true,
                KeepAliveInterval    = TimeSpan.Zero,
                SecWebSocketProtocol = "binary"
            };

            if (uri.IsDefaultPort)
            {
                var builder = new UriBuilder(uri)
                {
                    Port = Port
                };
                uri = builder.Uri;
            }

            var       clientFactory = new WebSocketClientFactory();
            WebSocket webSocket     = await clientFactory.ConnectAsync(uri, options);

            return(new WebsocketConnection(webSocket));
        }
        const int BUFFER_SIZE = 1 * 1024 * 1024 * 1024; // 1GB

        public async Task Run()
        {
            var factory = new WebSocketClientFactory();
            var uri     = new Uri("ws://localhost:27416/chat");
            var options = new WebSocketClientOptions()
            {
                KeepAliveInterval = TimeSpan.FromMilliseconds(500)
            };

            using (WebSocket webSocket = await factory.ConnectAsync(uri, options))
            {
                // receive loop
                Task readTask = Receive(webSocket);

                // send a message
                await Send(webSocket);

                // initiate the close handshake
                await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None);

                // wait for server to respond with a close frame
                await readTask;
            }
        }
예제 #7
0
 public WebSocketAdapter(WebSocketClientOptions options)
 {
     _options = options;
 }
예제 #8
0
 public WebSocketAdapter(WebSocketClientOptions options, int sendTimeoutSec, int maxMessageReadSize)
 {
     _maxMessageReadSize = maxMessageReadSize;
     _options            = options;
     _sendTimeoutSec     = TimeSpan.FromSeconds(sendTimeoutSec);
 }
 public WebSocketAdapter(WebSocketClientOptions options, int sendTimeoutSec)
 {
     _options        = options;
     _sendTimeoutSec = TimeSpan.FromSeconds(sendTimeoutSec);
 }