Sam*_*Sam 4 signalr signalr.client asp.net-core asp.net-core-signalr
我对SignalR. 显然,有什么称呼它如一些争论onClosed(),closed()等等。
在SignalR客户端的侦听器中,我正在尝试实现此事件,但不断收到错误消息,指出它不是函数。我试过onClosed()和closed()。同样的错误。如何检测客户端的关闭事件?
const signalRListener = () => {
connection.on('new_message', message => {
// Handle incoming message.
// This is working fine.
})
connection.closed(e => {
// Try to restart connection but I never get here to due error I'm receiving
})
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
这是我开始连接的方式:
export const signalRStart = (token) => {
connection = new signalR.HubConnectionBuilder()
.withUrl("/chat?access_token=" + token)
.configureLogging(signalR.LogLevel.Information)
.build();
// Set connection time out
connection.serverTimeoutInMilliseconds = 30000;
// Start connection
connection.start();
// Invoke listener
signalRListener();
}
Run Code Online (Sandbox Code Playgroud)
作为最佳实践,请
connection.start在connection.on收到任何消息之前调用,以便您的处理程序注册。
export const signalRStart = (token) => {
const connection = new signalR.HubConnectionBuilder()
.withUrl("/chat?access_token=" + token)
.configureLogging(signalR.LogLevel.Information)
.build();
// Set connection time out
connection.serverTimeoutInMilliseconds = 30000;
//register listeners
//Registers a handler that will be invoked when the hub method with the specified method name is invoked.
connection.on('new_message', message => {
// Handle incoming message.
// This is working fine.
});
//Registers a handler that will be invoked when the connection is closed.
connection.onclose(e => {
// ...
});
// Start connection
connection.start();
};
Run Code Online (Sandbox Code Playgroud)
参考ASP.NET Core SignalR JavaScript 客户端
参考SignalR JavaScript API - HubConnection 类
| 归档时间: |
|
| 查看次数: |
2666 次 |
| 最近记录: |