SignalR 协议协商

环境搭建

参考: .NET SignalR - 掘金 (juejin.cn)

协商过程

  • SignalR支持多种服务器推送方式:

    WebsocketServerSent Events长轮询

    默认按顺序尝试。

  • F12查看协商过程。

    初始化连接时客户端会发送请求,询问支持列表

  • image.png

    服务器返回支持列表

    image.png

    客户端收到返回信息后,切换协议

    image.png

    查看协商过程

    image.png

    协商问题

    当使用集群时,建立通讯和协商的对象不一致,会导致混乱

    image.png

    取消协商

    const connection = new signalR.HubConnectionBuilder()
      // .withUrl("https://localhost:7253/MyHub")
      .withUrl("https://localhost:7253/MyHub",{
            skipNegotiation:true,
            transport:signalR.HttpTransportType.WebSockets
        })
      .withAutomaticReconnect() //断线自动重连
      .build();
    connection.start();
    

    image.png

    设置后直接发送请求,没有协商过程

    image.png