在 React Native 中丢失互联网后如何重新连接 Hub?
How can i reconnect Hub after lost internet in React native?
当我的应用程序丢失并重新连接到互联网时,我无法再次重新连接到 SignalR Hub。除非我关闭应用程序并再次打开。
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();
谁能教我丢失后如何重新连接 SignalR Hub?谢谢!
你可以在 React-native
中使用 NetInfo
import { NetInfo } from "react-native"
已弃用。请改用 react-native-community/react-native-netinfo
。
您可以 运行 npm install --save @react-native-community/netinfo
和 react-native link @react-native-community/netinfo
import NetInfo from "@react-native-community/netinfo";
componentDidMount() {
NetInfo.isConnected.addEventListener('connectionChange', this.handleConnectionChange);
}
componentWillUnmount() {
NetInfo.isConnected.removeEventListener('connectionChange', this.handleConnectionChange);
}
handleConnectionChange = (isConnected) => {
this.setState({ status: isConnected });
if (isConnected !== "none" || isConnected !== "unknown") {
hub.start();
}
}
所以我使用 hub connectionState
检查并调用 hub.start()
它运行良好
当我的应用程序丢失并重新连接到互联网时,我无法再次重新连接到 SignalR Hub。除非我关闭应用程序并再次打开。
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();
谁能教我丢失后如何重新连接 SignalR Hub?谢谢!
你可以在 React-native
中使用NetInfo
import { NetInfo } from "react-native"
已弃用。请改用 react-native-community/react-native-netinfo
。
您可以 运行 npm install --save @react-native-community/netinfo
和 react-native link @react-native-community/netinfo
import NetInfo from "@react-native-community/netinfo";
componentDidMount() {
NetInfo.isConnected.addEventListener('connectionChange', this.handleConnectionChange);
}
componentWillUnmount() {
NetInfo.isConnected.removeEventListener('connectionChange', this.handleConnectionChange);
}
handleConnectionChange = (isConnected) => {
this.setState({ status: isConnected });
if (isConnected !== "none" || isConnected !== "unknown") {
hub.start();
}
}
所以我使用 hub connectionState
检查并调用 hub.start()
它运行良好