ImediaseeUvaultCDNInfluxis
FlashComGuru
 
 

  Active Topics    Memberlist    Search    Help
  Register  Login
Code snippets
 Flashcomguru Forums | Code snippets
Subject Topic: client side keep-alive script. Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
p0miki
Flashcom Newbie
Flashcom Newbie


Joined: 09 January 2004
Posts: 9
Posted: 10 January 2004 at 13:37 | IP Logged Quote p0miki

a simpe code to get around the flashcom bug of disconnecting a client if it doesn't send data over long time.

Code:

client_nc.lastCallTime = new Date().getTime();
keepAliveIntervalID = setInterval(connectionKeepAlive, 10000);
waitingForReply = false;

function connectionKeepAlive()
{
     trace(new Date() + " - keep alive interval has been reached.");
     //ping only if we are not still waiting for the previous keep alive message to return.
     if (waitingForReply == false)
     {
               currentTime = new Date().getTime();
               deltaTime = currentTime - client_nc.lastCallTime;
               trace("currentTime = " + currentTime + ", client.lastCallTime = " + client_nc.lastCallTime + ", deltaTime = " + deltaTime + " .");
               if ((deltaTime > 45000) && (client_nc.isConnected))
               {
                    trace( "calling   server keep alive.");
                    waitin gForReply   = true;
                    client _nc.lastCall Time = currentTime;
                    client _nc.call("cl ientKeepAliv e", new handleKeepAlive());
               }
               else
               {
                    trace( "no   need to send ping since client made connection with the server not so long ago.");
               }
     }
     else
     {
               trace(new Date() + " - still waiting for previous keep alive message to return. skipping keep alive call for now.")
     }
}

function handleKeepAlive()
{
     this.onResult = function(retVal)
     {
               waitingForReply = false;
               trace("keep alive is ok. result was = " + retVal + " .");
     }
     
     this.onStatus = function(infoObj)
     {
               waitingForReply = false;
               trace("FAILED to get keepAlive . code = " + infoObject.code + ", level = " + infoObject.level + " .");
     }
}   

On the server side put:
Client.prototype.clientKeepAlive = function()
{
     return true;
}


What the script will do is, every 10 seconds check when was the last time the client connection send data to the server. if more than 45 seconds have passed without sending data than the client will call a dummy function on the server inorder have them exchange some data (aka - ping).
I would advise to update the client_nc.lastCallTime property elsewhere in your client side script whenever you call a server side function, since if you call to a server on your own, there is no reason to ping it as well over a very shot while.

Edited by stoem on 12 January 2004 at 08:56
Back to Top View p0miki's Profile Search for other posts by p0miki
 
arckid
Flashcom Badass
Flashcom Badass
Avatar

Joined: 05 January 2004
Location: India
Posts: 483
Posted: 10 January 2004 at 22:02 | IP Logged Quote arckid

thanks for the cute code ... :)

__________________
Ashvin Savani (arckid)

Avinashi

Flash Platform Developer
Back to Top View arckid's Profile Search for other posts by arckid Visit arckid's Homepage
 
Ultimate
Moderator
Moderator
Avatar
Super Flashcommer

Joined: 05 January 2004
Location: Canada
Posts: 1130
Posted: 11 January 2004 at 11:03 | IP Logged Quote Ultimate

That's pretty long script...
Here's mine:

_global.getPingLoginSO = function (nc) {
     pingSO = SharedObject.getRemote("CLIENTPINGER", nc.uri, false);
     pingSO.onSync = function() {
           trace("ping");
     };
     pingSO.connect(nc);
     clearInterval(pingServer);
     _global.pingServer = setInterval(pingIt, 30000);
};

_global.pingIt = function () {
     if (conn_nc.isConnected == false) {
           clearInterval(pingServer);
     } else {
           pingSO.data.loggedInDate = new Date();
     }
};

I call the getPingLoginSO() function after I successfully connect up. That's it. In every file I have up.

__________________
Blog: http://www.sti-media.com
Community Site: http://www.FMSGuru.com
Game: Checkers
Back to Top View Ultimate's Profile Search for other posts by Ultimate Visit Ultimate's Homepage
 
designtaco
Flashcom Newbie
Flashcom Newbie
Avatar

Joined: 29 July 2004
Posts: 1
Posted: 29 July 2004 at 01:53 | IP Logged Quote designtaco

Ultimate wrote:

I call the getPingLoginSO() function after I successfully connect up. That's it. In every file I have up.


That's a nice snippet. Since simpleConnect initiates 2 connections (when rtmp specified) then kills one, how would you see the best way would be to integrate this code into the component?
Back to Top View designtaco's Profile Search for other posts by designtaco
 
Ultimate
Moderator
Moderator
Avatar
Super Flashcommer

Joined: 05 January 2004
Location: Canada
Posts: 1130
Posted: 29 July 2004 at 03:37 | IP Logged Quote Ultimate

Sorry, I really don't have any idea at the moment becauase I never use the components. In the beginning I tried to understand how they worked and what I could do with them but gave up because it was just easier in th end to create my own.

__________________
Blog: http://www.sti-media.com
Community Site: http://www.FMSGuru.com
Game: Checkers
Back to Top View Ultimate's Profile Search for other posts by Ultimate Visit Ultimate's Homepage
 
stoem
Big Kahuna
Big Kahuna
Avatar
stoem == Stefan

Joined: 05 January 2004
Location: United Kingdom
Posts: 1079
Posted: 29 July 2004 at 07:42 | IP Logged Quote stoem

I use this on the SS:

Code:

pingClient = function (){
     for (var i =0; i<application.clients.length; i++){
     var stillThere = application.clients.ping();
     if(stillThere == false){
           application.disconnect(application.clients );     
     }
     }
}



I run this on a setInterval (every x seconds) via onAppStart, seems to do the job.

stoem




__________________
Flashcomguru.com
Back to Top View stoem's Profile Search for other posts by stoem Visit stoem's Homepage
 
xbrotherx
Flashcom Newbie
Flashcom Newbie


Joined: 25 October 2004
Posts: 13
Posted: 25 October 2004 at 05:09 | IP Logged Quote xbrotherx

Stoem,

sorry to jump in on this conversation late, but I think the problem I am having with not being able to resume playback of video after pausing for over 5 minutes or so is related to the time-out whatchamacallit on the server.

So I tried implementing your solution but I am not sure it worked. Can you confirm that my SS script is correct below:

Code:

application.onAppStart = function(){
     keepAlive = setInterval(pingClient, 10000);
}

pingClient = function (){
     for (var i =0; i<application.clients.length; i++){
           var stillThere = application.clients.ping();
           if(stillThere == false){
                 application.disconnect(application.clients );      
          }
     }
}
Back to Top View xbrotherx's Profile Search for other posts by xbrotherx
 
stoem
Big Kahuna
Big Kahuna
Avatar
stoem == Stefan

Joined: 05 January 2004
Location: United Kingdom
Posts: 1079
Posted: 25 October 2004 at 08:45 | IP Logged Quote stoem

yeah that code looks fine but I think you should also use Ultimate's client side script, this will hold the connection open. My SS script won't do that, all that does is clean up clients that seem to be connected but in reality are long gone.

HTH

stoem


__________________
Flashcomguru.com
Back to Top View stoem's Profile Search for other posts by stoem Visit stoem's Homepage
 
Ultimate
Moderator
Moderator
Avatar
Super Flashcommer

Joined: 05 January 2004
Location: Canada
Posts: 1130
Posted: 25 October 2004 at 08:47 | IP Logged Quote Ultimate

It should look like this I think:

pingClient = function (){
     for (var i =0; i<application.clients.length; i++){
            var stillThere = application.clients[i].ping();
            if(stillThere == false){
                  application.discon nect(application.clients[i] );       
           }
     }
}

Stoem forgot to turn off forum codes when posting code... on his own forum!


__________________
Blog: http://www.sti-media.com
Community Site: http://www.FMSGuru.com
Game: Checkers
Back to Top View Ultimate's Profile Search for other posts by Ultimate Visit Ultimate's Homepage
 
stoem
Big Kahuna
Big Kahuna
Avatar
stoem == Stefan

Joined: 05 January 2004
Location: United Kingdom
Posts: 1079
Posted: 25 October 2004 at 09:06 | IP Logged Quote stoem

ah cr*p

__________________
Flashcomguru.com
Back to Top View stoem's Profile Search for other posts by stoem Visit stoem's Homepage
 
xbrotherx
Flashcom Newbie
Flashcom Newbie


Joined: 25 October 2004
Posts: 13
Posted: 25 October 2004 at 12:26 | IP Logged Quote xbrotherx

Wow, thanks Stoem and Ultimate!! Man if this stuff does what I hope it will do. I can actually go outside and see the sun soon!!

Thanks again.
Back to Top View xbrotherx's Profile Search for other posts by xbrotherx
 
xbrotherx
Flashcom Newbie
Flashcom Newbie


Joined: 25 October 2004
Posts: 13
Posted: 26 October 2004 at 02:37 | IP Logged Quote xbrotherx

Ultimate, if I may, I need to ask a potentially obvious question. I assume your "keep-alive" script runs in the client (the swf) and not on the server in a .asc file? Am I correct?
Back to Top View xbrotherx's Profile Search for other posts by xbrotherx
 
Ultimate
Moderator
Moderator
Avatar
Super Flashcommer

Joined: 05 January 2004
Location: Canada
Posts: 1130
Posted: 26 October 2004 at 02:41 | IP Logged Quote Ultimate

That is correct.

__________________
Blog: http://www.sti-media.com
Community Site: http://www.FMSGuru.com
Game: Checkers
Back to Top View Ultimate's Profile Search for other posts by Ultimate Visit Ultimate's Homepage
 
xbrotherx
Flashcom Newbie
Flashcom Newbie


Joined: 25 October 2004
Posts: 13
Posted: 26 October 2004 at 02:54 | IP Logged Quote xbrotherx

Thanks again. Testing now. :)
Back to Top View xbrotherx's Profile Search for other posts by xbrotherx
 
xbrotherx
Flashcom Newbie
Flashcom Newbie


Joined: 25 October 2004
Posts: 13
Posted: 26 October 2004 at 04:08 | IP Logged Quote xbrotherx

Ultimate, your script works great!! Thanks!
Back to Top View xbrotherx's Profile Search for other posts by xbrotherx
 
kevinz
Flashcom Newbie
Flashcom Newbie
Avatar

Joined: 06 May 2004
Location: Canada
Posts: 10
Posted: 30 October 2004 at 00:21 | IP Logged Quote kevinz

Hi Ultimate and Stoem,

Something weird happened to me, pls help me and give me some ideas .

I have written a similiar code like Stoem's in main.asc , but it doesn't work. The first thing weird is the ping() function return true for a long time after i have broken the internet connection of the test client machine. It supposed to return false , right ?

So i has to change from check the result of ping() to check the ping roundtrip time of the return of client.getStats().

The second weird thing happened, When i try to disconnect the client , the application.disconnect() return true. But the 'ghost' client is still there ! I can't get rid of this ghost client.

Any ideas !

The code as following :


function pingTimer(){
     for (var i=0; i< application.clients.length; i++)
     {
        trace("result of ping(): "+application.clients.ping());
         stats = application.clients.getStats();
         trace( "Ping roundtrip time: " + stats.ping_rtt);
           if (stats.ping_rtt > 20000){
               trace("result of disconnect(): "+application.disconnect(application.clients));

           }
     }
     return ;
}

application.pingTimerObj=setInterval(pingTimer,15000);

Kevin

Back to Top View kevinz's Profile Search for other posts by kevinz
 

Page of 2 Next >>
  Post ReplyPost New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum

Powered by Web Wiz Forums version 7.6
Copyright ©2001-2003 Web Wiz Guide


   all contents © Flashcomguru.com - Flash® is a trademark of Adobe® 1995-2007