signalR

作者: 李霖弢 | 来源:发表于2019-08-22 16:08 被阅读0次

    查看文档

    创建连接
    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();
    });
    

    相关文章

      网友评论

          本文标题:signalR

          本文链接:https://www.haomeiwen.com/subject/pionsctx.html