关于typescript中使用async await

import { LocalStorage } from "./identify/localStorage";

function getUserIP(onNewIP:(ip:string) => any) { //  onNewIp - your listener function for new IPs
    //compatibility for firefox and chrome
    var window_obj:any = window;
    var myPeerConnection =  window_obj.webkitRTCPeerConnection || window_obj.mozRTCPeerConnection ||  window_obj.RTCPeerConnection;
    var pc = new myPeerConnection({
        iceServers: []
    }),/*  */
    noop = function () { },
    localIPs:any = {},
    ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
    key;

    function iterateIP(ip:any) {
        if (!localIPs[ip] ) 
            return onNewIP(ip);
        localIPs[ip] = true;
    }

    //create a bogus data channel
    pc.createDataChannel("");

    // create offer and set local description
    pc.createOffer().then(function (sdp:any) {
        sdp.sdp.split('\n').forEach(function (line:any) {
            if (line.indexOf('candidate') < 0) return;
            line.match(ipRegex).forEach(iterateIP);
        });

        pc.setLocalDescription(sdp, noop, noop);
    }).catch(function (reason:any) {
        // An error occurred, so handle the failure to connect
    });

    //sten for candidate events
    pc.onicecandidate = function (ice:any) {
        if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
        ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
    };
}

 // Usage
/* let ipp;
getUserIP(function (ip) {
    ipp =  ip;
    console.log("ipp: ",ipp);
}); 
 */
class IpAsync {
    constructor() {
    }

    public async GetAsync(): Promise {
        var promise = new Promise(resolve => {
            getUserIP(res => {
                resolve(res);
            });
        });
        return promise;
    }
}

export async function getIpAsync() {
    let ha = new IpAsync();
    let res = await ha.GetAsync();
    let client_ip_stroage = new LocalStorage("Allpass_client_info_ip");
    //client_ip_stroage.del();return;
    let client_info_ip = client_ip_stroage.get();
    if(client_info_ip){
        return;
    }else{
        let client_info_stroage = new LocalStorage("Allpass_client_info");
        let client_info = client_info_stroage.get();

        if(!client_info){
            return;
        }else{
            let client_info_obj = JSON.parse(client_info);
            if(client_info_obj.data.ip){
                return;
            }else{
                //1.update allpass_client_info
                client_info_obj.data.ip = res;
                console.log("1111111:",client_info_obj);
                client_info_stroage.set(JSON.stringify(client_info_obj));

                //2.update allpass_client_ip
                client_ip_stroage.set(res);
            }
        } 
    }
}





 

你可能感兴趣的:(typescript)