【问题标题】:Signal R concurrent connection issueSignalr 并发连接问题
【发布时间】:2014-12-17 08:58:27
【问题描述】:

我正在使用 Signal R 在我的网页中开发聊天。我在同时连接时遇到问题。当两个浏览器同时连接到聊天时(当然,在秒数上会有很小的差异),连接函数同时处理请求(不是一个接一个),并且在每个聊天中复制一个客户端名称(所以connect函数连接同一个客户端两次)

我该如何解决这个问题? 提前谢谢!

我正在添加 Connect 函数的代码以及我使用的 onConnected 和 OnNewuserConnected 脚本(这是将用户添加到用户列表的位置)

public void Connect(string userName, int? pid)
    {
        var id = Context.ConnectionId;
        var projectId = pid.ToString();

        if (ConnectedUsers.Where(x => x.ConnectionId == id).Where(y => y.ProjectId == projectId).Count() == 0)
        {
            ConnectedUsers.Add(new UserDetail { ConnectionId = id, UserName = userName, ProjectId = projectId });
            // send to caller
            Clients.Caller.onConnected(id, userName, ConnectedUsers, CurrentMessage, projectId);

            // send to all except caller client
            Clients.AllExcept(id).onNewUserConnected(id, userName, projectId);

            // add user to group
            Groups.Add(id, projectId);

        }

这是客户端的 Javascript

    // Calls when user successfully logged in
chatHub.client.onConnected = function (id, userName, allUsers, messages, pid) {

    var inRoles = [];
    //// Add All Users
    $("li.userinRole_" + pid).each(function (index) {
        inRoles.push($(this).text());
    });
    $('#hdId_' + pid).val(id);
    $('#hdUserName_' + pid).val(userName);
    $('#spanUser_' + pid).html(userName);
    AddUser(chatHub, id, userName, pid);
    if ($.inArray(userName, inRoles) === -1) {
        console.log(userName + ": è un user online che non ha un ruolo in progetto ")
        for (var i = 0; i < allUsers.length; i++) {
            if ($.inArray(allUsers[i].UserName, inRoles) !== -1 && allUsers[i].ProjectId == pid) {
                AddUser(chatHub, allUsers[i].ConnectionId, allUsers[i].UserName, pid);
            }
        }

    } else {

        console.log(userName + ": è un user online con ruolo in progetto")
        for (i = 0; i < allUsers.length; i++) {
            if (allUsers[i].ProjectId == pid && allUsers[i].UserName != userName) {
                AddUser(chatHub, allUsers[i].ConnectionId, allUsers[i].UserName, pid);
            }

        }
    }

    // Add Existing Messages
    for (i = 0; i < messages.length; i++) {
        if (messages[i].Group == pid) {
            AddMessage(messages[i].UserName, messages[i].Message, pid);
        }
    }

}

// On New User Connected
chatHub.client.onNewUserConnected = function (id, name, pid) {
    debugger;
    var inRoles = [];
    //// Add All Users
    $("li.userinRole_" + pid).each(function (index) {
        inRoles.push($(this).text());
    });

        if (($.inArray(name, inRoles) !== -1) && ($.inArray($("#currentUser_" + pid).attr("value"), inRoles) == -1)) {
            console.log(name + ": ha un ruolo in questo progetto, add user cikli 1 new user");
            AddUser(chatHub, id, name, pid);
        }
        else if (($.inArray(name, inRoles) == -1) && ($.inArray($("#currentUser_" + pid).attr("value"), inRoles) !== -1)) {
            console.log(name + ": non ha un ruolo,add user cikli 2 new user");
            AddUser(chatHub, id, name, pid);

        }
        else if (($.inArray(name, inRoles) !== -1) && ($.inArray($("#currentUser_" + pid).attr("value"), inRoles) !== -1)) {
            console.log(name + ": non ha un ruolo in progetto " + pid);
            AddUser(chatHub, id, name, pid);
        }
}

问题是当两个用户同时连接时。 connect 函数填充 ConnectedUsers 列表(它占两个用户),这些用户由 onConnect 函数中的 javascript 添加(它们都添加到聊天中,因为它们都连接到同一个 projectId)。但是,除了这样做之外,在调用 onConnected 函数后,onNewUserConnected 函数会在每个聊天中增加一个用户...

【问题讨论】:

  • 如果不查看代码,我们将无法为您提供帮助。能否贴出相关代码
  • 我编辑了它,并添加了代码

标签: asp.net connection signalr chat


【解决方案1】:

什么是调用 .net Connect 方法?您的代码流中可能存在缺陷。因此,如果您从 MVC 控制器的构造函数中调用 Connect(),那么我可以看到您如何接收 2 条消息(它似乎正在建立 2x 连接)。再说一遍,Connect() 是从哪里调用的?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 2021-03-09
    相关资源
    最近更新 更多