我正在使用Signal R在我的网页上开发聊天。我遇到了同时连接的问题。当两个浏览器同时连接到聊天时(确保在几秒钟内存在小差异),连接功能同时处理请求(不是一个接一个),并且在每个聊天中复制客户端名称(所以connect函数连接同一个客户端两次)
我该如何解决这个问题? Thanx提前!
我正在添加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函数会为每个聊天添加一个用户...
答案 0 :(得分:0)
什么叫.net Connect方法?您的代码流可能存在缺陷。所以,如果你正在调用Connect(),让我们在MVC控制器的构造函数中说,那么我可以看到你如何接收2条消息(它似乎是连接2x)。那么,从哪里调用Connect()?