如何使用@aspnet/signalr 库修复 reactnative 中的“错误:无法启动传输‘WebSockets’:null”?

Ujj*_*har 8 signalr react-native

连接到服务器时,我一直无法启动传输 'WebSockets': null。

我正在 reactnative 移动应用程序中实现一个简单的聊天。我正在使用 @aspnet/signalr(version-1.1.4) 库。我收到此错误 - 错误:无法启动传输“WebSockets”:空。 在此处查看日志 我在 chrome 中得到以下日志

componentDidMount(){

try {

      const connection = new signalR.HubConnectionBuilder() //Connect to a hub
        .withUrl(baseUrl + `/chatbus`, {
          transport: signalR.HttpTransportType.WebSockets | signalR.HttpTransportType.LongPolling,
          'content-type': 'application/json',
          accessTokenFactory: () => authToken   //for authorization
        })
        .configureLogging(signalR.LogLevel.Debug)
        .build();

      connection.on('NotifySignalR', (ChatObject) => {
        console.log(ChatObject);

      });

      connection.start()
        .then(() => {
          console.log('Chat Connection started.');
          console.log('Now connected, connection ID=' + connection.id);
          this.initalAttemptForChat = true;
          connection.invoke("start");
        })
        .catch(() => {
          this.initalAttemptForChat = false;
          console.log('Error while establishing chatbus connection!');
        });

    } catch (e) {
      console.log("error ", e);
      throw e;
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 7

如果您在 Windows Server 中有您的应用程序,只需启用“WebSocket 协议”即可。

“添加或删除程序”>“打开或关闭 Windows 功能”>“Web 服务器(IIS)”>“Web 服务器”>“应用程序开发”>“WebSocket 协议”。