test_api.html 8.76 KB
<!--
* Copyright (C) 2012 Doubango Telecom <http://www.doubango.org>
* License: BSD
* This file is part of Open Source sipML5 solution <http://www.sipml5.org>
-->
<html>
<!-- head -->
<head>
    <title>Test API</title>
    <script src="release/SIPml-api.js" type="text/javascript"> </script>
</head>

<script type="text/javascript">

    var stack, registerSession, publishSession, subscribeSession, callSession, messageSession;

    window.onload = function () {
         SIPml.init(
             function(e){
                stack =  new SIPml.Stack({ realm: 'sip2sip.info', impi: '12121212', impu: 'sip:12121212@sip2sip.info', password: 'mysecret',
                    websocket_proxy_url: 'ws://192.168.0.9:5062',
                    events_listener: { events: '*', listener: function(e){
                                var bStarted = (e.type === 'started');
                                document.getElementById('btnRegister').disabled = !bStarted;
                                document.getElementById('btnSendMessage').disabled = !bStarted;
                                document.getElementById('btnPublishPresence').disabled = !bStarted;
                                document.getElementById('btnSubscribePresence').disabled = !bStarted;
                                document.getElementById('btnCall').disabled = !bStarted;
                            }
                        }
                });
                stack.start();
             }
         );
    }

    function onSessionEvent(e){
        console.info('session event = type = ' + e.type + ' - description = ' + e.description);
        if(e.type == 'terminated'){
            console.info('response code = ' + e.getSipResponseCode());

            if(e.session === registerSession){
                registerSession = null;
            }
            else if(e.session === messageSession){
                messageSession = null;
            }
            else if(e.session === publishSession){
                publishSession = null;
            }
            else if(e.session === subscribeSession){
                subscribeSession = null;
            }
            else if(e.session === callSession){
                callSession = null;
            }
        }
        else if(e.type == 'i_notify'){
            console.info('NOTIFY content = ' + e.getContentString());
            console.info('NOTIFY content-type = ' + e.getContentType());

            if (e.getContentType() == 'application/pidf+xml') {
                if (window.DOMParser) {
                    var parser = new DOMParser();
                    var xmlDoc = parser ? parser.parseFromString(e.getContentString(), "text/xml") : null;
                    var presenceNode = xmlDoc ? xmlDoc.getElementsByTagName ("presence")[0] : null;
                    if(presenceNode){
                        var entityUri = presenceNode.getAttribute ("entity");
                        var tupleNode = presenceNode.getElementsByTagName ("tuple")[0];
                        if(entityUri && tupleNode){
                            var statusNode = tupleNode.getElementsByTagName ("status")[0];
                            if(statusNode){
                                var basicNode = statusNode.getElementsByTagName ("basic")[0];
                                if(basicNode){
                                    console.info('Presence notification: Uri = ' + entityUri + ' status = ' + basicNode.textContent);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    function register(){
        if(registerSession){
            registerSession.unregister();
        }
        else{
            registerSession = stack.newSession('register', {
                                    expires: 200,
                                    events_listener: { events: '*', listener: onSessionEvent }
                                });
                                registerSession.register();
        }
    }

    function sendMessage(){
        if(!messageSession){
            messageSession = stack.newSession('message', {
                events_listener: { events: '*', listener: onSessionEvent }
            });
            messageSession.send('0039', 'Pche  la moule', 'text/plain;charset=utf-8');
        }
    }

    function publishPresence(){
        if(publishSession){
            publishSession.unpublish();
        }
        else{
            publishSession = stack.newSession('publish', {
                                expires: 200,
                                sip_headers: [
                                        { name: 'Event', value: 'presence' }
                                ],
                                events_listener: { events: '*', listener: onSessionEvent }
                            });
            var contentType = 'application/pidf+xml';
            var content = '<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n' +
                        '<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" xmlns:im=\"urn:ietf:params:xml:ns:pidf:im\"' +
             	            ' entity=\"sip:12121212@sip2sip.info\">\n' +
                            '<tuple id=\"s8794\">\n' +
                            '<status>\n'+
                            '   <basic>open</basic>\n' +
                            '   <im:im>away</im:im>\n' +
                            '</status>\n' +
                            '<contact priority=\"0.8\">tel:+33600000000</contact>\n' +
                            '<note  xml:lang=\"fr\">Bonjour de Paris :)</note>\n' +
                            '</tuple>\n' +
   	                    '</presence>';
            publishSession.publish(content, contentType);
       }
    }

    function subscribePresence(){
        if(subscribeSession){
            subscribeSession.unsubscribe();
        }
        else{
            subscribeSession = stack.newSession('subscribe', {
                            expires: 200,
                            events_listener: { events: '*', listener: onSessionEvent },
                            sip_headers: [
                                          { name: 'Event', value: 'presence' },
                                          { name: 'Accept', value: 'application/pidf+xml' }
                                ],
                            sip_caps: [
                                        { name: '+g.oma.sip-im', value: null },
                                        { name: '+audio', value: null },
                                        { name: 'language', value: '\"en,fr\"' }
                                ]
                        });
            subscribeSession.subscribe('13131313');
        }
    }

    function call() {
        if(callSession){
            callSession.hangup();
        }
        else{
            callSession = stack.newSession('call-audiovideo', {
                video_local: document.getElementById('video_local'),
                video_remote: document.getElementById('video_remote'),
                sip_caps: [
                                        { name: '+g.oma.sip-im' },
                                        { name: '+sip.ice' },
                                        { name: 'language', value: '\"en,fr\"' }
                                ],
                sip_headers: [
                                        { name: 'What', value: 'Audio/Video call', session: false },
                                        { name: 'Organization-2', value: 'Doubango Telecom', session: false }
                                ]
            });

            callSession.addEventListener(['connecting', 'connected', 'terminating', 'terminated'], function (e) {
                console.info('call event = ' + e.type);
            });

            callSession.call('+33600000000');
        }
    }

    function stop(){
        if(stack){
            stack.stop();

            stack = null;
            publishSession = null;
            registerSession = null;
            callSession = null;
        }
    }

</script>

<!-- body -->
<body>
    <form action=''>

    <input type="button" value="register" id="btnRegister" onclick='register();' disabled />
    <input type="button" value="sendMessage" id="btnSendMessage" onclick='sendMessage();' disabled />
    <input type="button" value="publishPresence" id="btnPublishPresence" onclick='publishPresence();' disabled />
    <input type="button" value="subscribePresence" id="btnSubscribePresence" onclick='subscribePresence();' disabled />
    <input type="button" value="call" id="btnCall" onclick='call();' disabled />

    <input type="button" value="stop" id="btnStop" onclick='stop();' />
    </form>
</body>
</html>