示例#1
0
        public async Task RunTest(byte[] content)
        {
            try
            {
                if (ConnectStatus == Status.Connected)
                {
                    await Connection.SendAsync("PerformanceTest", new MessageBody
                    {
                        CreatedTime = DateTimeOffset.UtcNow,
                        Contnet     = content
                    });

                    Interlocked.Increment(ref SendMessageStats.SuccessCount);
                }
                else
                {
                    Interlocked.Increment(ref SendMessageStats.NotSentCount);
                }
            }
            catch (Exception e)
            {
                SendMessageStats.AddException(e);

                Interlocked.Increment(ref SendMessageStats.ErrorCount);
            }
        }
示例#2
0
        private async Task ConnectCore()
        {
            ConnectStatus = Status.Connecting;
            var now = DateTimeOffset.UtcNow;

            if (now - _lastAuthTime > AuthExpire)
            {
                (string url, string accessToken) = await GetAuth();

                if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(accessToken))
                {
                    throw new ArgumentNullException("url or accesstoken is null");
                }

                if (Connection != null)
                {
                    Connection.Closed -= OnClosed;
                    // 1. dispose the old connection first
                    await Connection.DisposeAsync();

                    Connection = null;
                }

                var connection = new HubConnectionBuilder()
                                 .WithUrl(url, options =>
                                          //.WithUrl("http://localhost:8080/client?hub=gamehub", options =>
                {
                    options.AccessTokenProvider = () => Task.FromResult(accessToken);
                    options.CloseTimeout        = TimeSpan.FromSeconds(15);
                    options.Transports          = HttpTransportType.WebSockets;
                    options.SkipNegotiation     = true;
                })
                                 //.AddMessagePackProtocol()
                                 .Build();

                connection.Closed += OnClosed;

                connection.On("Connected", () =>
                {
                    ConnectStatus = Status.Connected;
                    Interlocked.Increment(ref ConnectStats.SuccessCount);

                    var elapsed = ConnectStats.SetElapsed(_lastConnectTime);
                });

                connection.On <MessageBody>("PerformanceTest", message =>
                {
                    Interlocked.Increment(ref SendMessageStats.ReceivedCount);
                    SendMessageStats.SetElapsed(message.CreatedTime);
                });

                // set the auth time after the connection is built
                _lastAuthTime = DateTime.UtcNow;
                Connection    = connection;
            }

            _lastConnectTime = DateTime.UtcNow;
            Interlocked.Increment(ref ConnectStats.TotalCount);
            await Connection.StartAsync();
        }