ReceiveMessage 不适用于 abp.signalR
ReceiveMessage does not work with abp.signalR
我正在使用 abp.signalR 但我遇到了与 ReceiveMessage 事件相关的问题,我想知道收到消息后我尝试了以下代码但没有任何反应:\
this.chatHub.on("ReceiveMessage", (username: string, message: string) => {
});
Web 控制台屏幕截图:
我的后端代码:
我需要向后端添加更多代码吗?
namespace HealthMapControlPanel.ChatAppService
{
public class MyChatHub : Hub, ITransientDependency
{
public IAbpSession AbpSession { get; set; }
public ILogger Logger { get; set; }
public MyChatHub()
{
AbpSession = NullAbpSession.Instance;
Logger = NullLogger.Instance;
}
public async Task SendMessage(string message)
{
await Clients.All.SendAsync("getMessage", string.Format("User {0}: {1}", AbpSession.UserId, "the message that has been sent from client is"+message));
}
public async Task SendToUser(string msg, long userId)
{
if (this.Clients != null)
{
await Clients.User(userId.ToString()).SendAsync("Send", msg, "From Server by userID ", Context.ConnectionId, Clock.Now);
}
else
{
throw new UserFriendlyException("something wrong");
}
}
public override async Task OnConnectedAsync()
{
await base.OnConnectedAsync();
Logger.Debug("A client connected to MyChatHub: " + Context.ConnectionId);
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await base.OnDisconnectedAsync(exception);
Logger.Debug("A client disconnected from MyChatHub: " + Context.ConnectionId);
}
}
}
这不起作用,因为您在客户端和服务器上订阅了 "ReceiveMessage"
,而您正在执行 .SendAsync("Send");
您应该在客户端上更改为:
this.chatHub.on("Send", (username: string, message: string) => {
});
或在服务器上:
await Clients.User(userId.ToString()).SendAsync("ReceiveMessage", msg, "From Server by userID ", Context.ConnectionId, Clock.Now);
更新:
this.chatHub.on("getMessage", (username: string, message: string) => {
});
我正在使用 abp.signalR 但我遇到了与 ReceiveMessage 事件相关的问题,我想知道收到消息后我尝试了以下代码但没有任何反应:\
this.chatHub.on("ReceiveMessage", (username: string, message: string) => {
});
Web 控制台屏幕截图:
我的后端代码: 我需要向后端添加更多代码吗?
namespace HealthMapControlPanel.ChatAppService
{
public class MyChatHub : Hub, ITransientDependency
{
public IAbpSession AbpSession { get; set; }
public ILogger Logger { get; set; }
public MyChatHub()
{
AbpSession = NullAbpSession.Instance;
Logger = NullLogger.Instance;
}
public async Task SendMessage(string message)
{
await Clients.All.SendAsync("getMessage", string.Format("User {0}: {1}", AbpSession.UserId, "the message that has been sent from client is"+message));
}
public async Task SendToUser(string msg, long userId)
{
if (this.Clients != null)
{
await Clients.User(userId.ToString()).SendAsync("Send", msg, "From Server by userID ", Context.ConnectionId, Clock.Now);
}
else
{
throw new UserFriendlyException("something wrong");
}
}
public override async Task OnConnectedAsync()
{
await base.OnConnectedAsync();
Logger.Debug("A client connected to MyChatHub: " + Context.ConnectionId);
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await base.OnDisconnectedAsync(exception);
Logger.Debug("A client disconnected from MyChatHub: " + Context.ConnectionId);
}
}
}
这不起作用,因为您在客户端和服务器上订阅了 "ReceiveMessage"
,而您正在执行 .SendAsync("Send");
您应该在客户端上更改为:
this.chatHub.on("Send", (username: string, message: string) => {
});
或在服务器上:
await Clients.User(userId.ToString()).SendAsync("ReceiveMessage", msg, "From Server by userID ", Context.ConnectionId, Clock.Now);
更新:
this.chatHub.on("getMessage", (username: string, message: string) => {
});