Can someone explain to me why I don't see conn

2019-08-26 04:26发布

I need help with ionic Network plugin. Here's a code, but it doesn't work for me. No console log or any other modals. No errors. at the top

import { Network } from '@ionic-native/network';

ionViewDidLoad() {
    this.getWarrentsNumber();
    let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
        console.log('network was disconnected :-(');
    });

    disconnectSubscription.unsubscribe();

    let connectSubscription = this.network.onConnect().subscribe(() => {
        console.log('network connected!');
        setTimeout(() => {
            if (this.network.type === 'wifi') {
                console.log('we got a wifi connection, woohoo!');
            }
        }, 3000);
    });

    connectSubscription.unsubscribe();
}

3条回答
别忘想泡老子
2楼-- · 2019-08-26 04:45

Declare your variables connectSubscription & disconnectSubscription as class properties and then try to unsubscribe() them in ioniViewWillLeave() hook instead of ionViewDidLoad(). So the code would look similar to the following -

connectSubscription: any;
disconnectSubscription: any;

ionViewDidLoad() {
    this.getWarrentsNumber();
    this.disconnectSubscription = this.network.onDisconnect().subscribe(() => {
        console.log('network was disconnected :-(');
    });

    this.connectSubscription = this.network.onConnect().subscribe(() => {
        console.log('network connected!');
        setTimeout(() => {
            if (this.network.type === 'wifi') {
                console.log('we got a wifi connection, woohoo!');
            }
        }, 3000);
    });
}

ioniViewWillLeave() {
    this.disconnectSubscription.unsubscribe();
    this.connectSubscription.unsubscribe();
}
查看更多
我命由我不由天
3楼-- · 2019-08-26 04:53

Here is the quote from plugin's official github repo

The online event fires when a previously unconnected device receives a network connection to allow an application access to the Internet. It relies on the same information as the Connection API, and fires when the connection.type changes from NONE to any other value.

As you can see onConnect will only emit something when previously unconnected device receives a network connection.

To check on startup if device is online you can directly check this.network.type

Or

You can create a service which will handle all these

@Injectable()
export class MyNetworkService implements OnInit {
  public connection: BehaviorSubject<string> = new BehaviorSubject(null);
  constructor(private network: Network, private platform: Platform) {

    this.connection = this.network.type;
    this.platform.ready().then(() => {
      this.network.onConnect().subscribe(() => {
        this.connection = 'something';
      });
      this.network.onDisconnect().subscribe(() => {
        this.connection = 'none';
      });
    });
  }

  ngOnInit() {
    this._setCurrentConnectionType();

    this.platform.ready().then(() => {
      this.network.onConnect().pipe(timer(3000)).subscribe(this._onConnectHandler);
      this.network.onDisconnect().pipe(timer(3000)).subscribe(this._onDisconnectHandler);
    });
  }

  private _onConnectHandler= () => this.connection.next(this.network.type);

  private _onDisconnectHandler = () => this.connection.next('offline');
}

And then you can inject your service where-ever you want and subscribe to connection.

constructor(private myNetworkService: MyNetworkService) {
  this.myNetworkService.connection.subscribe(type => {
    // you might filter out nulls
    // Before subscribe add .pipe(filter(type => type !== null))
  })

}
查看更多
We Are One
4楼-- · 2019-08-26 04:55

That code works properly but it works dependent platform like android or ios. It may not console anything on the browser I think. Please test your application with respect to the platform device.

or else you can use ngOnInit() instead of ionViewDidLoad();

查看更多
登录 后发表回答