@@ -25,6 +25,7 @@ import { connection } from '@kit.NetworkKit'
2525import { LogUtils } from '../../../../support/utils/LogUtils'
2626import { HippyAny } from '../../../../support/common/HippyTypes'
2727import { HippyModulePromise } from '../../HippyModulePromise'
28+ import { EventDispatcher } from '../../javascript/EventDispatcher'
2829
2930const NETINFO_MODULE_TAG = "NetInfoModule"
3031
@@ -133,6 +134,16 @@ export class NetInfoModule extends HippyNativeModuleBase {
133134 this.netConnection.register((error) => {
134135 LogUtils.d(NETINFO_MODULE_TAG, "registerReceiver: " + JSON.stringify(error))
135136 })
137+ // 订阅网络可用事件
138+ this.netConnection.on('netAvailable', (data: connection.NetHandle) => {
139+ LogUtils.d(NETINFO_MODULE_TAG, "Network available, netId: " + data.netId);
140+ this.handleNativeEvent();
141+ });
142+ // 订阅网络丢失事件
143+ this.netConnection.on('netLost', (data: connection.NetHandle) => {
144+ LogUtils.d(NETINFO_MODULE_TAG, "Network lost");
145+ this.handleNativeEvent();
146+ });
136147 } catch (e) {
137148 LogUtils.e("NetInfoModule", "registerReceiver: " + e.getMessage());
138149 }
@@ -147,4 +158,19 @@ export class NetInfoModule extends HippyNativeModuleBase {
147158 LogUtils.d("NetInfoModule", "unregisterReceiver: " + e.getMessage());
148159 }
149160 }
161+
162+ private handleNativeEvent() {
163+ if (this.ctx) {
164+ let manager = this.ctx.getModuleManager();
165+ if (manager) {
166+ let eventModule = manager.getJavaScriptModule(EventDispatcher.MODULE_NAME);
167+ if (eventModule) {
168+ let type = this.getCurrentConnectionType();
169+ let map = new Map<string, string>();
170+ map.set('network_info', type);
171+ (eventModule as EventDispatcher).receiveNativeEvent("networkStatusDidChange", map);
172+ }
173+ }
174+ }
175+ }
150176}
0 commit comments