【问题标题】:How to intercept, in react-native, the signalR error 'Error: Server timeout elapsed without receiving a message from the server.'?如何在 react-native 中拦截 signalR 错误“错误:服务器超时已过而未收到来自服务器的消息。”?
【发布时间】:2021-02-01 19:30:51
【问题描述】:

我在 react-native 中使用包 @aspnet/signalr 来连接我的服务器。

在应用程序处于前台之前一切正常,如果我失去连接而不会收到错误,我可以重新连接。

当我在后台长时间打开应用程序时,我可以立即重新连接到我的服务器,但我收到错误 Error: Connection disconnected with error 'Error: Server timeout elapsed without receiving a message from the server.'

如何拦截这个错误?

这是我的一段代码:

connection = new signalR.HubConnectionBuilder()
   .withUrl("http://192.168.xxx.xxx/notificationHub?userId=" + authInfo.userId)
   .build();
    
connection.on("receiveMessage", data => {
   console.log('*** MESSAGGIO RICEVUTO ***');
   Alert.alert(data);
});


                        
connection.start()
   .then(() => console.log("Connessione avvenuta"))
   .catch((err) => console.log(err);

connection.onclose()
   .then(() => connection.start(););

谢谢

【问题讨论】:

    标签: react-native signalr asp.net-core-signalr


    【解决方案1】:

    错误:连接因错误“错误:服务器超时而未收到来自服务器的消息而断开”。

    serverTimeoutInMilliseconds 的默认超时值为 30,000 毫秒(30 秒),如果超过此超时而没有收到来自服务器的任何消息,则连接可能会因上述错误而终止。

    要解决此问题,请检查您是否仅更新 SignalR 集线器的 KeepAliveInterval 设置,而不更改客户端上的 serverTimeoutInMilliseconds 值。

    推荐的serverTimeoutInMilliseconds 值是KeepAliveInterval 值的两倍。

    更新:

    有没有办法拦截这个错误并在没有警告的情况下管理错误?

    如果您不希望 signalR 客户端在浏览器控制台选项卡中记录此错误,您可以尝试将 LogLevel 修改为 None。

     .configureLogging(signalR.LogLevel.None)
    

    然后在onclose 回调中管理错误,如下所示。

    connection.onclose(error => {
        //...
    
        console.log("Connection Disconnected");
    });
    

    【讨论】:

    • 这没关系。我的问题不同:有没有办法拦截这个错误并在没有警告的情况下管理错误?
    • Is there a way to intercept this error and manage the error without warning?你可以查看我的更新。
    猜你喜欢
    • 2020-09-05
    • 2021-12-05
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    • 2015-12-18
    相关资源
    最近更新 更多