【问题标题】:Client not receiving messages in SignalR 2.2 app客户端未在 SignalR 2.2 应用程序中接收消息
【发布时间】:2019-09-27 06:11:31
【问题描述】:

在我的应用程序中,我想在用户登录但客户端未从集线器接收消息时更新客户端。

我的 Hub 类:

    public class GameHub : Hub
    {
        public async Task UserLoggedIn(string userName)
        {
            await Clients.All.SendAsync("UserLoggedIn", userName);
        }
    }

UserLoggedIn在用户登录时执行,我设置了一个断点,GameHub上的UserLoggedIn方法肯定被调用了。

在我的客户页面上

        const connection = new signalR.HubConnectionBuilder()
            .withUrl("/gameHub")
            .configureLogging(signalR.LogLevel.Information)
            .build();

        connection.start().then(() => {
            console.log("connected");
        });

        connection.on("UserLoggedIn", (userName) => {
            console.log("User Logged In" + userName);
        });

在另一个隐身浏览器的控制台窗口中,我看到“已连接”,但在 GameHub 上调用该方法后,我没有看到“用户登录”

谁能看到我在这里做错了什么?

【问题讨论】:

    标签: javascript c# typescript asp.net-core signalr


    【解决方案1】:

    您没有收到隐身浏览器的消息,因为您只收听信号器而不调用它,所以我会将您的代码更改为此

    public class GameHub : Hub
    {
        public async Task UserLoggedIn(string userName)
        {
            await Clients.All.SendAsync("SendUserLoggedInMessage", userName);
        }
    }
    

    然后在js代码中

    <script>
        const connection = new signalR.HubConnectionBuilder()
            .withUrl("/gameHub")
            .configureLogging(signalR.LogLevel.Information)
            .build();
    
        connection.start().then(() => {
            console.log("connected");
        });
    
        // listen on SendUserLoggedInMessage
        connection.on("SendUserLoggedInMessage", (userName) => {
            console.log("User Logged In" + userName);
        });
    
        // invoke UserLoggedIn method
       connection.send("userLoggedIn", username)
                  .then(() => console.log("something"));
    
    </script>
    

    【讨论】:

      猜你喜欢
      • 2016-08-25
      • 2013-05-21
      • 2020-08-13
      • 1970-01-01
      • 2017-05-26
      • 1970-01-01
      • 2020-10-15
      • 1970-01-01
      • 2016-04-29
      相关资源
      最近更新 更多