有人可以向我解释为什么我在此代码中看不到连接状态吗? [英] Can someone explain to me why I don't see connection status in this code?

查看:96
本文介绍了有人可以向我解释为什么我在此代码中看不到连接状态吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关离子网络插件的帮助.这是代码,但对我不起作用.没有控制台日志或任何其他模式.没有错误. 在顶部

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();
}

推荐答案

这是插件官方

在线事件在先前未连接的设备收到以下消息时触发 网络连接,以允许应用程序访问Internet.它 依赖于与Connection API相同的信息,并在出现以下情况时触发 connection.type从NONE更改为任何其他值.

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.

如您所见,onConnect仅在以前未连接的设备接收到网络连接时才会发出信号.

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

要在启动时检查设备是否在线,可以直接检查this.network.type

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

您可以创建将处理所有这些问题的服务

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))
  })

}

这篇关于有人可以向我解释为什么我在此代码中看不到连接状态吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆