Sign In Register

How can we help you today?

Start a new topic
Answered

Call cloud code on every request

I'm trying to develop a online SNS game, and I want to let the game check if the client is outdated or the server is closed. So, I want to make a cloud code that will be called whenever the player request anything, and if the client or the server is closed, it will do something like "Spark.setScriptData("error", true)" and don't let the client connect to the game.


I have tried to make a new event called "Check Update" and call it every time before calling anything else, but this waste a lot of time as it have to call two events and wait for the check update result. So I am figuring how to call a cloud code when player request anything, and " if(outdated)  ", it wont call the original request, but only return "Spark.setScriptData("error", true)" .


Thanks for helping.


Best Answer

Hi Chris,


You might find a module quite useful for this, if you navigate to Cloud Code in the Portal you'll find the module tab. If you create a new one that contains your check code in it you can call it by simply placing the following at the top of the Cloud Code for each event you'd like to run the check on.


require("moduleName");


Having this placed at the top of the Cloud Code for each of your events will run the module code before it runs any other Custom Code you may have in the event. In your module code if the checks fail, return the error. Using modules means you will only have to place "require("moduleName");" in each event rather than the full check function itself. This will help keep your cloud code scripts tidy and save time for you.


Thanks,

Liam





1 Comment

Answer

Hi Chris,


You might find a module quite useful for this, if you navigate to Cloud Code in the Portal you'll find the module tab. If you create a new one that contains your check code in it you can call it by simply placing the following at the top of the Cloud Code for each event you'd like to run the check on.


require("moduleName");


Having this placed at the top of the Cloud Code for each of your events will run the module code before it runs any other Custom Code you may have in the event. In your module code if the checks fail, return the error. Using modules means you will only have to place "require("moduleName");" in each event rather than the full check function itself. This will help keep your cloud code scripts tidy and save time for you.


Thanks,

Liam





Login to post a comment