";OnDisconnected():找不到合适的方法来覆盖“-”信号R
本文关键字:覆盖 方法 信号 OnDisconnected quot 找不到 | 更新日期: 2023-09-27 18:20:12
我一直在尝试通过遵循CodeProject上的"Asp.Net SignalR聊天室"教程来实现聊天室(http://www.codeproject.com/Articles/562023/Asp-Net-SignalR-Chat-Room)。然而,我得到了错误"Hubs.ChatHubs.OnDisconnected():找不到合适的方法来覆盖"
ChatHub类别:
public class ChatHub : Hub
{
#region Data Members
static List<UserDetail> ConnectedUsers = new List<UserDetail>();
static List<MessageDetail> CurrentMessage = new List<MessageDetail>();
#endregion
#region Methods
public void Connect(string userName)
{
var id = Context.ConnectionId;
if (ConnectedUsers.Count(x => x.ConnectionId == id) == 0)
{
ConnectedUsers.Add(new UserDetail { ConnectionId = id, UserName = userName });
// send to caller
Clients.Caller.onConnected(id, userName, ConnectedUsers, CurrentMessage);
// send to all except caller client
Clients.AllExcept(id).onNewUserConnected(id, userName);
}
}
public void SendMessageToAll(string userName, string message)
{
// store last 100 messages in cache
AddMessageinCache(userName, message);
// Broad cast message
Clients.All.messageReceived(userName, message);
}
public void SendPrivateMessage(string toUserId, string message)
{
string fromUserId = Context.ConnectionId;
var toUser = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == toUserId);
var fromUser = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == fromUserId);
if (toUser != null && fromUser != null)
{
// send to
Clients.Client(toUserId).sendPrivateMessage(fromUserId, fromUser.UserName, message);
// send to caller user
Clients.Caller.sendPrivateMessage(toUserId, fromUser.UserName, message);
}
}
public override System.Threading.Tasks.Task OnDisconnected()
{
var item = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
if (item != null)
{
ConnectedUsers.Remove(item);
var id = Context.ConnectionId;
Clients.All.onUserDisconnected(id, item.UserName);
}
return base.OnDisconnected();
}
#endregion
#region private Messages
private void AddMessageinCache(string userName, string message)
{
CurrentMessage.Add(new MessageDetail { UserName = userName, Message = message });
if (CurrentMessage.Count > 100)
CurrentMessage.RemoveAt(0);
}
#endregion
}
关于为什么会发生这种情况,有什么线索吗?
对于2.1.1版本+更改
public override Task OnDisconnected()
至
public override Task OnDisconnected(bool stopCalled)
您的signalR depedency中存在不匹配,他们在2.1.1 中更改了OnDisconnected的签名
因此,将所有项目升级到2.1.1或将所有项目降级到2.1.0,它应该可以工作