0

I want to setInterval connection check but it's not working for me. Can someone explain to me why and how can i do it? Thank you!

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

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

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

setInterval(NetInfo.getConnectionInfo().then((connectionInfo) => {
    alert(
       'Initial, type: ' +
        connectionInfo.type +
        ', effectiveType: ' +
       connectionInfo.effectiveType,
     );
  }),1000);

export default class ChatScreen extends Component { }
ravibagul91
  • 20,072
  • 5
  • 36
  • 59

1 Answers1

0

call the setInterval in componentDidMount()

Why are you not using the NetInfo event listener? is there a special reason?

NetInfo.addEventListener(state => {
  console.log("Connection type", state.type);
  console.log("Is connected?", state.isConnected);
});
Saadi
  • 1,292
  • 13
  • 22
  • Yes, because my hub.start(); must only call once. So i have to write it outside class component. But when the connection is lost i can't call hub.start(); again. – Nguyễn Minh Quân Ngô Sep 14 '19 at 07:30
  • You can very easily check the status of the connection with the listener. Each the connection changes (connects or disconnects), you can call a specific code. The listener callback is called each time the connection changes . – Saadi Sep 14 '19 at 08:26
  • Thank you, i know but if i check the status to call `hub.start();` in class component My `hub.start();` will conflict when i re-open it again – Nguyễn Minh Quân Ngô Sep 14 '19 at 09:38