Sign In Register

How can we help you today?

Start a new topic
Answered

Set session timeout

Is there anyway to expire a session after not getting requests from client for more than 5 minutes, and get the time when the session expires??


Best Answer

I can think of 2 ways, and I'm sure there are more, but the following would be the way I tackle this:


Every time a request is made (in cloud code) cancel the previous SparkScheduler task and reschedule it to call a module in 5 minutes. Then if the module ever gets called you know no request was made in 5 minutes. You could get the current time in the module and call Spark.getPlayer().disconnect(true). 


So you could made an "ExpirationManagerModule":

var ExpirationManager = new function(){
	
	this.scheduleExpiration = function(){
		Spark.getScheduler().inSeconds("DisconnectionModule", 5*60, null, "disconnection_scheduler");
	}

	this.cancelExpiration = function(){
		Spark.getScheduler().cancel("disconnection_scheduler");
	}

}

Then you'd make a DisconnectionModule that simply called: Spark.getPlayer().disconnect(false);


 Then in any cloud code requests/events you would add:

 

require("ExpirationManager");

ExpirationManager.cancelExpiration();

//Your custom code here

ExpirationManager.scheduleExpiration();


 Anyway, just one of the ways to do this. Hopefully it helps.



No problem. Glad I could help!

Hey Ryan.


Yes that helps a lot. It is exactly what I was looking for. You have my gratitude.

Oh and as far as get the time when the session expires, you could just call Date.now() inside the DisconnectionModule, though I'm not sure where you want to store that (you could set it to the player's scriptData or a custom collection).

Answer

I can think of 2 ways, and I'm sure there are more, but the following would be the way I tackle this:


Every time a request is made (in cloud code) cancel the previous SparkScheduler task and reschedule it to call a module in 5 minutes. Then if the module ever gets called you know no request was made in 5 minutes. You could get the current time in the module and call Spark.getPlayer().disconnect(true). 


So you could made an "ExpirationManagerModule":

var ExpirationManager = new function(){
	
	this.scheduleExpiration = function(){
		Spark.getScheduler().inSeconds("DisconnectionModule", 5*60, null, "disconnection_scheduler");
	}

	this.cancelExpiration = function(){
		Spark.getScheduler().cancel("disconnection_scheduler");
	}

}

Then you'd make a DisconnectionModule that simply called: Spark.getPlayer().disconnect(false);


 Then in any cloud code requests/events you would add:

 

require("ExpirationManager");

ExpirationManager.cancelExpiration();

//Your custom code here

ExpirationManager.scheduleExpiration();


 Anyway, just one of the ways to do this. Hopefully it helps.


Login to post a comment