cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              
<h3>Auth chat</h3>
<div class="note">
    <p><a href="https://github.com/CppComet/auth-example">Git repository with all code of this example.</a></p> 
    <p>In this example show how authenticate on CppComet and how send personal messages to users via CppComet</p> 
    
    <ul>
        <li><a href="http://comet-server.org/doku.php/en" target="_blank">More info about CppComet</a></li>
        <li><a href="http://comet-server.org/doku.php/en:comet:cometql"  target="_blank">More info about CometQL</a></li>
        <li><a href="http://comet-server.org/doku.php/en:comet:authentication"  target="_blank">More info about auth in CppComet</a></li> 
    </ul>
    
    <p>
        Open the example in two or more browsers, Copy your `USER_ID` from one 
        window and paste it into another window in the field labeled 
        'Identificator of user who must will receive the message` 
        enter the message text and press send. You will see that the message 
        will come only to the window with the same USER_ID you specified when 
        sending it.
    </p> 
</div>
<div class="root"> 
    <hr>
    <div><span id="userId"></span> - use this id to send message</div>
    <hr>
    <div class="status">
        We are waiting for authorization on the comet server
    </div>
    <hr>
    <input type="text" placeholder="Message" id="text"> - Personal text message for user<br>
    <input type="text" placeholder="user_id" id="to_user_id"> - Identificator of user who must will receive the message<br>
    <button onclick="send($('#to_user_id').val(), $('#text').val())">Send</button>
    <hr>
    <div id="log"></div>
</div>
 
            
          
!
            
              
hr{
    clear:both;
}

.hide{
    display:none;
}

#log{
    color:#000
}
.status{
    color:#272;
    font-weight:bold;
}

.note{
    background:#eee;
    padding:1px 10px;
    margin:5px;
    border-radius:5px;
    text-align:justify;
}
            
          
!
            
              
function log()
{
    console.log(arguments);
    $("#log").append(JSON.stringify(arguments)+"<hr>");
}

// Authorization on comets server
function auth()
{
    var user_id = 0;
    
    try{
        user_id = localStorage['user_id']
    }catch(e){ 
        user_id = Math.floor(Math.random()*1000000)
    } 
     
    if(!user_id)
    {
        user_id = Math.floor(Math.random()*1000000)
        localStorage['user_id'] = user_id
    }

    $("#userId").text("USER_ID = "+user_id);
    log("Request authorization for user_id="+user_id)
    var user_key = "PassForUser"+user_id

    //  The first step – it is sending user’s ID and a random hash in your system to the comet-server via CometQL.
    //  auth.php ( https://github.com/CppComet/auth-example/blob/master/auth.php )
    return $.when($.ajax({
        url: "https://comet-server.com/doc/CometQL/auth-chat/auth.php?user_id="+user_id+"&user_key="+user_key+"&t="+Math.floor(Math.random()*1000000),
        type: "GET",
        dataType:'json',
    })).always(function(res){
        $(".status").html("We are waiting for connection to the comet server");
        
        // The function start accepts connection settings and opens new connection. 
        // https://comet-server.com/wiki/doku.php/en:comet:javascript_api#connection_with_server
        cometApi.start({
            node: "app.comet-server.ru",
            dev_id: 15,
            user_id:user_id,
            user_key:user_key,
        })
    }).promise();
}

/**
 * 
 * @param {integer} user_id  
 * @param {string} text 
 */
function send(user_id, text)
{
    // Send message
    // send.php ( https://github.com/CppComet/auth-example/blob/master/send.php )
    $.ajax({
        url: "https://comet-server.com/doc/CometQL/auth-chat/send.php?user_id="+user_id+"&msg="+text+"&t="+Math.floor(Math.random()*1000000),
        type: "GET", 
    })
}

cometApi.onAuthSuccess(function()
{
    // We will initialize the call after successful authorization on the comet server.
    $(".StartCallBtn").removeClass("hide");
    log("Authorization on the comet server is complete")
    $(".status").html("Authorization on the comet server is complete");
    cometApi.getTrackPipeUsers("track_authExample", function(){
        log(e)
    })
})

cometApi.subscription("msg.message", function(e){ 
    log(e)
})

cometApi.subscription("track_authExample.subscription", function(msg)
{
    log(msg) 
});	

cometApi.subscription("track_authExample.unsubscription", function(msg)
{
    log(msg) 
});	

auth();

            
          
!
999px
Loading ..................

Console