datso / react-native-pjsip

A PJSIP module for React Native.
http://datso.github.io/react-native-pjsip
GNU General Public License v3.0
273 stars 231 forks source link

why am i get call_changed and call_terminated Listener together #149

Open origani opened 5 years ago

origani commented 5 years ago

let configuration = { "name": "admin", "username": "admin", "domain": "**", "password": "**", "proxy": null, "transport": null, // Default TCP "regServer": null, // Default wildcard "regTimeout": null, // Default 3600 "regHeaders": { "X-Custom-Header": "Value" }, "regContactParams": ";unique-device-token-id=XXXXXXXXX", "regOnAdd": true, // Default true, use false for manual REGISTRATION };

    let state = await endpoint.start({
        service: {
            ua: Platform.select({ios: "XZSIP iOS", android: "XZSIP Android"})
        },
        network: {
            useWifi: true,
            useOtherNetworks: true
        }
    });

    endpoint.changeCodecSettings({
        "G722/16000/1": 100
    });

    const {accounts, calls, settings: endpointSettings, connectivity} = state

    // Subscribe to endpoint events
    endpoint.on("registration_changed", (account) => {

    });
    endpoint.on("connectivity_changed", (available) => {

    });
    endpoint.on("call_received", (call) => {
        console.warn('電話')
    });
    endpoint.on("call_changed", (call) => {

    });
    endpoint.on("call_terminated", (call) => {

    });
    endpoint.on("call_screen_locked", (call) => {

    });

    if(accounts[0]){
        endpoint.deleteAccount(accounts[0]).then(async () => {
            account = await endpoint.createAccount(configuration);
        });
    }else{
        account=await endpoint.createAccount(configuration);
    } 

let options = { headers: { "P-Assserted-Identity": "Header example", "X-UA": "React native" } };

    console.warn(account)
    let call = await endpoint.makeCall(account, '********', options);
    console.warn(call)
    call.getId(); // Use this id to detect changes and make actions

    endpoint.addListener("call_changed", (newCall) => {
        if (call.getId() === newCall.getId()) {
            call.getMedia();
            console.warn('接通了')
        }
    });

    endpoint.addListener("call_terminated", (newCall) => {
        if (call.getId() === newCall.getId()) {
            console.warn('挂断了')
        }
    });