Check internet connection in web using Angular 4

后端 未结 10 2078
感情败类
感情败类 2020-12-15 19:37

I am using Angular 4 for my application development. I need to check If the network connection is available for its have any Connection issue using Angular for. Is it possib

相关标签:
10条回答
  • 2020-12-15 20:05
    if(navigator.onLine){
      alert("You are Online")
     }
      else {
       alert("You are Offline")
        }
    

    navigator.onLine --> returns true or false

    0 讨论(0)
  • 2020-12-15 20:05

    Detecting internet connection status in Angular application

    We can achieve desired feature using window.ononline and window.onoffline events.

    You can install service via npm command:

    npm i ng-connection-service
    

    Subscribe to monitor() method to get push notification whenever internet connection status is changed

    import { Component } from '@angular/core';
    import { ConnectionService } from 'ng-connection-service';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      status = 'ONLINE';
      isConnected = true;
    
      constructor(private connectionService: ConnectionService) {
        this.connectionService.monitor().subscribe(isConnected => {
          this.isConnected = isConnected;
          if (this.isConnected) {
            this.status = "ONLINE";
          }
          else {
            this.status = "OFFLINE";
          }
        })
      }
    }
    
    0 讨论(0)
  • 2020-12-15 20:06

    You do not have to use any library for this, you can use navigator global object like window. You can use in in angular4

    public onlineOffline: boolean = navigator.onLine;
    
    0 讨论(0)
  • 2020-12-15 20:12

    There is an option to check if your browser is online/offline via navigator.onLine The thing is, if navigator.onLine returns false, it means you are offline. But, returning true does not 100% make sure you are online. The reason is very simple and that is the implementation of the browser.

    From the official documentation link Navigator.onLine

    Browsers implement this property differently.

    In Chrome and Safari, if the browser is not able to connect to a local area network (LAN) or a router, it is offline; all other conditions return true. So while you can assume that the browser is offline when it returns a false value, you cannot assume that a true value necessarily means that the browser can access the internet. You could be getting false positives, such as in cases where the computer is running a virtualization software that has virtual ethernet adapters that are always "connected." Therefore, if you really want to determine the online status of the browser, you should develop additional means for checking

    In Firefox and Internet Explorer, switching the browser to offline mode sends a false value. Until Firefox 41, all other conditions return a true value; testing actual behavior on Nightly 68 on Windows shows that it only looks for LAN connection like Chrome and Safari giving false positives.

    0 讨论(0)
提交回复
热议问题