客户端未在 SignalR 2.2 应用程序中接收消息
Client not receiving messages in SignalR 2.2 app
在我的应用程序中,我想在用户登录但客户端未从集线器接收消息时更新客户端。
我的中心 class:
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);
});
在另一个隐身浏览器的控制台 window 中,我看到 "connected",但在 GameHub
[=16= 上调用该方法后我看不到 "User Logged In" ]
谁能看出我做错了什么?
你没有收到隐身浏览器的消息,因为你只听信号器而不调用它所以我会把你的代码改成这个
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>
在我的应用程序中,我想在用户登录但客户端未从集线器接收消息时更新客户端。
我的中心 class:
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);
});
在另一个隐身浏览器的控制台 window 中,我看到 "connected",但在 GameHub
[=16= 上调用该方法后我看不到 "User Logged In" ]
谁能看出我做错了什么?
你没有收到隐身浏览器的消息,因为你只听信号器而不调用它所以我会把你的代码改成这个
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>