ReceiveMessage不能与abp.signalR一起工作。

问题描述 投票:-1回答:1

我正在使用abp.signalR,但我遇到了与ReceiveMessage事件有关的问题,我想知道一旦消息被接收,我尝试了下面的代码,但没有发生任何事情:。

this.chatHub.on("ReceiveMessage", (username: string, message: string) => { 


});

Web控制台的截图。

enter image description here

我的后台代码我是否需要在后台添加更多代码?


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);
    }


    }


}

typescript signalr aspnetboilerplate asp.net-boilerplate
1个回答
1
投票

这是不工作的,因为你订阅了 "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) => { 
});
© www.soinside.com 2019 - 2024. All rights reserved.