查看文档
创建连接
const connection = new signalR.HubConnectionBuilder()
.withUrl("/chatHub")
.configureLogging(signalR.LogLevel.Information)
.build();
connection.start().then(function () {
console.log("connected");
});
发送消息
connection.invoke("SendMessage", user, message)
.then(d => console.log(d.msg))
.catch(err => console.error(err.toString()));
监听事件
connection.on("ReceiveMessage", (user, message) => {
...
});
重连
async function start() {
try {
await connection.start();
console.log("connected");
} catch (err) {
console.log(err);
setTimeout(() => start(), 5000);
}
};
connection.onclose(async () => {
await start();
});
网友评论