javascriptcordovabrowsercordova-pluginsphonertc

phonertc is not defined - Cordova phonertc on browser


I'm trying to build a test app using the phonertc plugin for apache cordova. I'm following the wiki, so i've created the turn server, and i've implemented a socket.io signaling server. All those servers are working.

But when i try build a demo app, after creating the project, adding platforms and the plugin, copying from the wiki,

var config = {
    isInitiator: true,
    turn: {
        host: 'turn:turn.example.com:3478',
        username: 'test',
        password: '123'
    },
    streams: {
        audio: true,
        video: false
    }
}

var session = new phonertc.Session(config);

i get error on

var session = new phonertc.Session(config);

while running the demo on chrome. The error is

main.js:27 Uncaught ReferenceError: phonertc is not defined

The complete version of the index.html is

<!DOCTYPE html>

<html>
    <head>
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>Hello World</title>
    </head>
    <body>
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script src="js/socket.io-1.4.5.js"></script>
        <script src="js/main.js"></script>
        <script>
            document.addEventListener('deviceready', function() {
                doAll();
            });
        </script>
    </body>
</html>

index.js and cordova.js are the dafault from the cordova hello world, main.js is

var socket;
var config;
var session;
function doAll(){
    socket;
    function setup(){
        socket = io.connect("http://192.168.1.121:3000");
        socket.emit("add-user", {"userCode": "1"});

    }
    setup();


    config = {
        isInitiator: true,
        turn: {
            host: 'turn:192.168.1.121:3478',
            username: 'aaaa',
            password: 'bbbb'
        },
        streams: {
            audio: true,
            video: false
        }
    }

    session = new phonertc.Session(config);
    session.on('sendMessage', function (data) { 
        socket.emit("private-message",{content:data,userCode:"2"});
    });
    socket.on("add-message", function(data){

            console.log(data.content);
            session.receiveMessage(JSON.parse(data.content));
        });
    session.on('answer', function () { 
        console.log('Other client answered!');
    });

    session.on('disconnect', function () { 
        console.log('Other client disconnected!');
    });
    session.call();
}

I know, the code is horrible, but it is just a demo. How do i solve the reference to phonertc? Do i need to import something on in the index.html? Thanks


Solution

  • Ok, i changed the line

    var session = new phonertc.Session(config);
    

    to

    var session = new cordova.plugins.phonertc.Session(config);
    

    now it works.