React native 在React native中丢失internet后,如何重新连接集线器?

React native 在React native中丢失internet后,如何重新连接集线器?,react-native,signalr,React Native,Signalr,当我的应用程序丢失并重新连接到internet时,我无法再次重新连接到信号集线器。除非我关闭应用程序并再次打开 import React, { Component } from 'react'; import { StyleSheet, TouchableWithoutFeedback, Keyboard, Text, View, TouchableOpacity, Image, Alert, TextInput, FlatList, Button, RefreshControl } from

当我的应用程序丢失并重新连接到internet时,我无法再次重新连接到信号集线器。除非我关闭应用程序并再次打开

import React, { Component } from 'react';
import { StyleSheet, TouchableWithoutFeedback, Keyboard, Text, View, TouchableOpacity, Image, Alert, TextInput, FlatList, Button, RefreshControl } from 'react-native';

import * as signalR from '@aspnet/signalr';

const hubUrl = '/chatHub';
let hub = new signalR.HubConnectionBuilder()
    .withUrl(hubUrl)
    .configureLogging(signalR.LogLevel.Information)
    .build();
hub.start();

有人能教我丢失后如何重新连接信号机集线器吗?谢谢大家!

您可以在React native中使用
NetInfo

从“react native”导入{NetInfo}
已弃用。改用
react native community/react native netinfo

您可以运行
npm安装--save@react native community/netinfo
react native link@react native community/netinfo

从“@react native community/NetInfo”导入NetInfo;
componentDidMount(){
NetInfo.isConnected.addEventListener('connectionChange',this.handleConnectionChange);
}
组件将卸载(){
NetInfo.isConnected.removeEventListener('connectionChange',this.handleConnectionChange);
}
handleConnectionChange=(未连接)=>{
this.setState({status:isConnected});
如果(未连接!=“无”|未连接!=“未知”){
hub.start();
}
}

所以我使用
集线器连接状态
来检查并调用
集线器启动()
它工作得很好

谢谢你的回复:)我已经以不同的方式使用了Netinfo,但它不起作用。但我会尝试你的方式,并尽快回复你!很抱歉再次打扰你,但有点不对劲。我把你的代码放在我的聊天屏幕上,它第一次工作,第二次出错:握手响应。我想把它修好,但没用。你能解释一下第二次怎么不行吗?Thanks@Nguyễ这是另一个问题。创建并发布新问题,并详细解释。@NguyễnMinhQu–nNgô为什么取消选中我的答案?很抱歉,当我重新打开屏幕并崩溃时,它会调用
hub.start()
。基本上,重新连接不起作用。我一直在用不同的方式重新连接它。所以请理解我,它真的不起作用