nativescript-dna-netservices
Version:
NativeScript plugin for Bonjour/ZeroConf and network monitoring. RxJS based APIs for Android and iOS.
110 lines • 4.99 kB
JavaScript
import { Observable, of } from 'rxjs';
import { finalize, mergeMap } from 'rxjs/operators';
import * as Connectivity from '@nativescript/core/connectivity';
import { addressType, networkType } from './netservice.common';
export class NetworkMonitorService {
static monitorNetwork() {
const netMon = new Observable(observer => {
observer.next(NetworkMonitorService.getNetworkStatus());
Connectivity.startMonitoring((newConnectionType) => {
observer.next(NetworkMonitorService.getNetworkStatusFromType(newConnectionType));
});
return () => {
Connectivity.stopMonitoring();
};
});
return netMon.pipe(mergeMap(value => value), finalize(() => Connectivity.stopMonitoring()));
}
static getWiFiIpAddress() {
const at = addressType.IPv4;
const addresses = NetworkMonitorService.getInterfaceCardIpAddress("en0", at);
return of(addresses.length ? addresses[0].address : "");
}
static getCellularIpAddress() {
const at = addressType.IPv4;
let addrs = NetworkMonitorService.getInterfaceCardIpAddress("pdp_ip0", at);
if (!addrs.length)
addrs = NetworkMonitorService.getInterfaceCardIpAddress("pdp_ip1", at);
else if (!addrs.length)
addrs = NetworkMonitorService.getInterfaceCardIpAddress("pdp_ip2", at);
else if (!addrs.length)
addrs = NetworkMonitorService.getInterfaceCardIpAddress("pdp_ip3", at);
return of(addrs.length ? addrs[0].address : "");
}
static getNetworkStatus() {
return NetworkMonitorService.getNetworkStatusFromType(Connectivity.getConnectionType());
}
static dumpIpAddress() {
const ipAddrObserable = new Observable((observer) => {
let addresses = NetworkMonitorService.getInterfaceCardIpAddress();
observer.next(addresses);
observer.complete();
});
return ipAddrObserable;
}
static getNetworkStatusFromType(cType) {
switch (cType) {
case Connectivity.connectionType.wifi:
return NetworkMonitorService.getWiFiIpAddress().pipe(mergeMap(ipAddr => of({
connType: networkType.wifi,
ipAddress: ipAddr
})));
case Connectivity.connectionType.mobile:
return NetworkMonitorService.getCellularIpAddress().pipe(mergeMap(ipAddr => of({
connType: networkType.cellular,
ipAddress: ipAddr
})));
default:
return of({ connType: networkType.none, ipAddress: "" });
}
}
static getInterfaceCardIpAddress(interfaceCardName, ipAddressType) {
const MAX_ADDRLEN = 46;
let addresses = [];
let iPtrPtr = new interop.Reference();
if (getifaddrs(iPtrPtr) === 0) {
const interfacesPtr = iPtrPtr.value;
let tempAddrPtr = interfacesPtr;
while (tempAddrPtr != null) {
let sinAddr = null;
const addr = tempAddrPtr.value;
const adapterName = NSString.stringWithUTF8String(addr.ifa_name).toString();
const sa = new interop.Reference(sockaddr, addr.ifa_addr).value;
const sinFamily = sa.sa_family;
if (sinFamily === 2) {
sinAddr = interop.handleof(addr.ifa_addr).add(4);
}
else if (sinFamily === 30) {
sinAddr = interop.handleof(addr.ifa_addr).add(8);
}
if (sinAddr) {
const addrStr = interop.alloc(MAX_ADDRLEN);
const result = inet_ntop(sinFamily, sinAddr, addrStr, MAX_ADDRLEN);
if (result) {
const addr = NSString.stringWithUTF8String(addrStr).toString();
let address = {
address: addr,
type: sinFamily,
adapterName: adapterName
};
if (interfaceCardName == null && ipAddressType == null) {
addresses.push(address);
}
else if (adapterName === interfaceCardName && ipAddressType == null) {
addresses.push(address);
}
else if (adapterName === interfaceCardName && ipAddressType === sinFamily) {
addresses = [];
addresses.push(address);
return addresses;
}
}
}
tempAddrPtr = addr.ifa_next;
}
freeifaddrs(interfacesPtr);
}
return addresses;
}
}
//# sourceMappingURL=network-monitor-service.ios.js.map