Ejemplo n.º 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}");
                }
            });
        }
Ejemplo n.º 2
0
        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}");
                    }
                }
            }
        }