Exemple #1
0
        private async Task DirectConnectionJoinGroup(string connectionString)
        {
            var serviceManager = new ServiceManagerBuilder().WithOptions(option =>
            {
                option.ConnectionString     = connectionString;
                option.ServiceTransportType = ServiceTransportType.Transient;
            }).Build();

            var hubContext = await serviceManager.CreateHubContextAsync(SignalRConstants.DefaultRestHubName);

            await SignalRUtils.JoinGroupForConnection(
                _totalConnection,
                _groupCount,
                _connectionIndex,
                async (i, g) =>
            {
                var userId = SignalRUtils.GenClientUserIdFromConnectionIndex(_connectionIndex[i]);
                try
                {
                    await hubContext.UserGroups.AddToGroupAsync(
                        userId,
                        SignalRUtils.GroupName(_type, g));
                    _statisticsCollector.IncreaseJoinGroupSuccess();
                }
                catch (Exception e)
                {
                    _statisticsCollector.IncreaseJoinGroupFail();
                    Log.Error($"Fail to join group: {e.Message}");
                }
            });
        }
Exemple #2
0
        protected override async Task SendMessages(IEnumerable <Package> packages)
        {
            var hubContext = await CreateHubContextAsync();

            await Task.WhenAll(from package in packages
                               let index         = ConnectionIndex[package.LocalIndex]
                               let groupIndex    = index % GroupCount
                               let indexInGroup  = index / GroupCount
                               let connection    = package.Connection
                               let data          = package.Data
                               let restApiClient = hubContext
                               where Mode == SignalREnums.GroupConfigMode.Group &&
                               IsSending(indexInGroup, GroupInternalModulo, GroupInternalRemainderBegin, GroupInternalRemainderEnd) &&
                               IsSending(groupIndex, GroupCount, GroupLevelRemainderBegin, GroupLevelRemainderEnd) ||
                               Mode == SignalREnums.GroupConfigMode.Connection &&
                               IsSending(index, Modulo, RemainderBegin, RemainderEnd)
                               select ContinuousSend((GroupName: SignalRUtils.GroupName(Type, index % GroupCount),
                                                      RestApiProvider: restApiClient),
                                                     data,
                                                     SendToGroup,
                                                     TimeSpan.FromMilliseconds(Duration),
                                                     TimeSpan.FromMilliseconds(Interval),
                                                     TimeSpan.FromMilliseconds(1),
                                                     TimeSpan.FromMilliseconds(Interval)));
        }
        private async Task DirectConnectionLeaveGroup(string connectionString)
        {
            var serviceManager = new ServiceManagerBuilder().WithOptions(option =>
            {
                option.ConnectionString     = connectionString;
                option.ServiceTransportType = ServiceTransportType.Transient;
            }).Build();

            var hubContext = await serviceManager.CreateHubContextAsync(SignalRConstants.DefaultRestHubName);

            if (_connections.Count >= _groupCount)
            {
                for (var i = 0; i < _connections.Count; i++)
                {
                    var userId = SignalRUtils.GenClientUserIdFromConnectionIndex(_connectionIndex[i]);
                    try
                    {
                        await hubContext.UserGroups.RemoveFromGroupAsync(userId,
                                                                         SignalRUtils.GroupName(_type, _connectionIndex[i] % _groupCount));

                        _statisticsCollector.IncreaseLeaveGroupSuccess();
                    }
                    catch (Exception e)
                    {
                        _statisticsCollector.IncreaseLeaveGroupFail();
                        Log.Error($"Fail to leave group: {e.Message}");
                    }
                }
            }
            else
            {
                for (var i = 0; i < _groupCount; i++)
                {
                    var userId = SignalRUtils.GenClientUserIdFromConnectionIndex(i);
                    try
                    {
                        await hubContext.UserGroups.RemoveFromGroupAsync(
                            userId,
                            SignalRUtils.GroupName(_type, i));

                        _statisticsCollector.IncreaseLeaveGroupSuccess();
                    }
                    catch (Exception e)
                    {
                        _statisticsCollector.IncreaseLeaveGroupFail();
                        Log.Error($"Fail to leave group: {e.Message}");
                    }
                }
            }
        }
Exemple #4
0
 private async Task NormalConnectionJoinGroup()
 {
     await SignalRUtils.JoinGroupForConnection(
         _totalConnection,
         _groupCount,
         _connectionIndex,
         async (i, g) =>
     {
         await SignalRUtils.JoinToGroup(
             _connections[i],
             SignalRUtils.GroupName(_type, g),
             _statisticsCollector);
     });
 }
 private async Task NormalConnectionLeaveGroup()
 {
     if (_connections.Count >= _groupCount)
     {
         await Task.WhenAll(from i in Enumerable.Range(0, _connections.Count)
                            select SignalRUtils.LeaveFromGroup(_connections[i],
                                                               SignalRUtils.GroupName(_type,
                                                                                      _connectionIndex[i] % _groupCount),
                                                               _statisticsCollector));
     }
     else
     {
         var connectionCount = _connections.Count;
         await Task.WhenAll(from i in Enumerable.Range(0, _groupCount)
                            select SignalRUtils.LeaveFromGroup(_connections[i % connectionCount],
                                                               SignalRUtils.GroupName(_type, i),
                                                               _statisticsCollector));
     }
 }
Exemple #6
0
        protected virtual IEnumerable <Package> GenerateData()
        {
            // Generate necessary data
            var messageBlob = SignalRUtils.GenerateRandomData(MessageSize);
            // TODO: for group count is larger than connections, we need to tune groupName parameters
            var packages = from i in Enumerable.Range(0, Connections.Count)
                           let groupName = SignalRUtils.GroupName(Type, ConnectionIndex[i] % GroupCount)
                                           select
                                           new Package
            {
                LocalIndex = i,
                Connection = Connections[i],
                GroupName  = groupName,
                Data       = new Dictionary <string, object>
                {
                    { SignalRConstants.MessageBlob, messageBlob },
                    { SignalRConstants.GroupName, groupName }
                }
            };

            return(packages);
        }
Exemple #7
0
        protected override IEnumerable <Package> GenerateData()
        {
            // Generate necessary data
            var messageBlob = SignalRUtils.GenerateRandomData(MessageSize);

            var packages = from i in Enumerable.Range(0, Connections.Count)
                           let groupName = SignalRUtils.GroupName(Type, ConnectionIndex[i] % GroupCount)
                                           select
                                           new Package
            {
                LocalIndex = i,
                Connection = Connections[i],
                GroupName  = groupName,
                Data       = new Dictionary <string, object>
                {
                    { SignalRConstants.MessageBlob, messageBlob },                    // message payload
                    { SignalRConstants.GroupName, groupName },
                    { _isIngroup, false }
                }
            };

            return(packages);
        }
 private async Task NormalConnectionJoinGroup()
 {
     if (_connections.Count >= _groupCount)
     {
         for (var i = 0; i < _connections.Count; i++)
         {
             await SignalRUtils.JoinToGroup(
                 _connections[i],
                 SignalRUtils.GroupName(_type, _connectionIndex[i] % _groupCount),
                 _statisticsCollector);
         }
     }
     else
     {
         var connectionCount = _connections.Count;
         for (var i = 0; i < _groupCount; i++)
         {
             await SignalRUtils.JoinToGroup(
                 _connections[i % connectionCount],
                 SignalRUtils.GroupName(_type, i),
                 _statisticsCollector);
         }
     }
 }