Timer Service

Timer Service facilitates developers to create and maintain server side timer in their app/game. APIs create and maintain the status of the timer on App42 Cloud so that developers don’t have to maintain it on the client side. Developers don’t have to write any business logic, the timer just needs to be configured. Using this service one can create, delete, start, stop and check the status of the timer. Sample use case: timers in strategy games.

Import Statement
  • create User Api for Android
  • create User Api for Windows
  • create User Api for iOS
  • create User Api for Swift
  • create User Api for Java
  • create User Api for Unity
  • create User Api for JS
  • create User Api for Corona
  • create User Api for Cocos2DX
  • create User Api for .Net
  • create User Api for PHP
  • create User Api for Marmalade
  •  create User Api for Ruby
  •  create User Api for Flash
  1. #include "App42API.h"  
Initialize

In order to use the various functions available in a specific API, the developer has to initialize with App42API by passing the apiKey and the secretKey which will become available after the app creation from AppHQ dashboard.

Required Parameters

apiKey - The Application key given when the application was created. secretKey - The secret key corresponding to the application key given when the application was created.

  • create User Api for Android
  • create User Api for Windows
  • create User Api for iOS
  • create User Api for Swift
  • create User Api for Java
  • create User Api for Unity
  • create User Api for JS
  • create User Api for Corona
  • create User Api for Cocos2DX
  • create User Api for .Net
  • create User Api for PHP
  • create User Api for Marmalade
  •  create User Api for Ruby
  •  create User Api for Flash
  1. App42API::Initialize("API_KEY""SECRET_KEY");  
Build Service

After initialization, the developer will have to call the buildXXXService method on App42API to get the instance of the particular API that they wish to build. For example, to build an instance of TimerService, buildTimerService() method needs to be called.

  • create User Api for Android
  • create User Api for Windows
  • create User Api for iOS
  • create User Api for Swift
  • create User Api for Java
  • create User Api for Unity
  • create User Api for JS
  • create User Api for Corona
  • create User Api for Cocos2DX
  • create User Api for .Net
  • create User Api for PHP
  • create User Api for Marmalade
  •  create User Api for Ruby
  •  create User Api for Flash
  1. TimerService *timerService = App42API::BuildTimerService();  
Create Or Update Timer

Create a new timer and also update the time of existing timer.

Required Parameters

timerName - Name of the timer which has to be created. timeInSeconds - Times in seconds upto which timer will remain active.

  • create User Api for Android
  • create User Api for Windows
  • create User Api for iOS
  • create User Api for Swift
  • create User Api for Java
  • create User Api for Unity
  • create User Api for JS
  • create User Api for Corona
  • create User Api for Cocos2DX
  • create User Api for .Net
  • create User Api for PHP
  • create User Api for Marmalade
  •  create User Api for Ruby
  •  create User Api for Flash
  1. const char* timerName = "<Enter_your_timer_name>";  
  2. long timeInSeconds = 1000;  
  3. App42API::Initialize("API_KEY""SECRET_KEY");  
  4. TimerService *timerService = App42API::BuildTimerService();  
  5. timerService->CreateOrUpdateTimer(timerName,timeInSeconds,app42callback(Sample_Class::onTimerServiceRequestCompleted, this));  
  6.   
  7. void Sample_Class::onTimerServiceRequestCompleted( void *response)  
  8. {  
  9.     App42TimerResponse *app42TimerResponse = (App42TimerResponse*)response;  
  10.     if(app42TimerResponse->isSuccess)  
  11.     {  
  12.         printf("\ncode=%d",app42TimerResponse->getCode());  
  13.         printf("\nisSuccess=%d",app42TimerResponse->isSuccess);  
  14.         printf("\nResponse Body=%s",app42TimerResponse->getBody().c_str());  
  15.         printf("\n TimerName =%s",app42TimerResponse->app42Timer.name.c_str());  
  16.         printf("\n timeInSeconds=%ld",app42TimerResponse->app42Timer.timeInSeconds);  
  17.     }  
  18.     else  
  19.     {  
  20.         printf("\nerrordetails:%s",app42TimerResponse->errorDetails.c_str());  
  21.         printf("\nerrorMessage:%s",app42TimerResponse->errorMessage.c_str());  
  22.         printf("\nappErrorCode:%d",app42TimerResponse->appErrorCode);  
  23.         printf("\nhttpErrorCode:%d",app42TimerResponse->httpErrorCode);  
  24.     }  
  25. }  
Start Timer

Start the timer count down for a particular user.

Required Parameters

timerName - Name of the timer which has to be started. userName - Name of the user whose timer count down has to be started.

  • create User Api for Android
  • create User Api for Windows
  • create User Api for iOS
  • create User Api for Swift
  • create User Api for Java
  • create User Api for Unity
  • create User Api for JS
  • create User Api for Corona
  • create User Api for Cocos2DX
  • create User Api for .Net
  • create User Api for PHP
  • create User Api for Marmalade
  •  create User Api for Ruby
  •  create User Api for Flash
  1. const char* timerName = "<Enter_your_timer_name>";  
  2. const char* userName = "John";  
  3. App42API::Initialize("API_KEY""SECRET_KEY");  
  4. TimerService *timerService = App42API::BuildTimerService();  
  5. timerService->StartTimer(timerName,userName,app42callback(Sample_Class::onTimerServiceRequestCompleted, this));  
  6.   
  7. void Sample_Class::onTimerServiceRequestCompleted( void *response)  
  8. {  
  9.     App42TimerResponse *app42TimerResponse = (App42TimerResponse*)response;  
  10.     if(app42TimerResponse->isSuccess)  
  11.     {  
  12.         printf("\ncode=%d",app42TimerResponse->getCode());  
  13.         printf("\nisSuccess=%d",app42TimerResponse->isSuccess);  
  14.         printf("\nResponse Body=%s",app42TimerResponse->getBody().c_str());  
  15.         printf("\n TimerName =%s",app42TimerResponse->app42Timer.name.c_str());  
  16.         printf("\n userName =%s",app42TimerResponse->app42Timer.userName.c_str());  
  17.         printf("\n currentTime =%s",app42TimerResponse->app42Timer.currentTime.c_str());  
  18.         printf("\n startTime =%s",app42TimerResponse->app42Timer.startTime.c_str());  
  19.         printf("\n endTime =%s",app42TimerResponse->app42Timer.endTime.c_str());  
  20.         printf("\n isTimerActive=%d",app42TimerResponse->app42Timer.isTimerActive);  
  21.     }  
  22.     else  
  23.     {  
  24.         printf("\nerrordetails:%s",app42TimerResponse->errorDetails.c_str());  
  25.         printf("\nerrorMessage:%s",app42TimerResponse->errorMessage.c_str());  
  26.         printf("\nappErrorCode:%d",app42TimerResponse->appErrorCode);  
  27.         printf("\nhttpErrorCode:%d",app42TimerResponse->httpErrorCode);  
  28.     }  
  29. }  
Active Status of Timer

Check the status of timer whether it is active or not.

Required Parameters

timerName - Name of the timer which has to be checked. userName - Name of the user for whom you want check the status of timer.

  • create User Api for Android
  • create User Api for Windows
  • create User Api for iOS
  • create User Api for Swift
  • create User Api for Java
  • create User Api for Unity
  • create User Api for JS
  • create User Api for Corona
  • create User Api for Cocos2DX
  • create User Api for .Net
  • create User Api for PHP
  • create User Api for Marmalade
  •  create User Api for Ruby
  •  create User Api for Flash
  1. const char* timerName = "<Enter_your_timer_name>";  
  2. const char* userName = "John";  
  3. App42API::Initialize("API_KEY""SECRET_KEY");  
  4. TimerService *timerService = App42API::BuildTimerService();  
  5. timerService->IsTimerActive(timerName, userName, app42callback(Sample_Class::onTimerServiceRequestCompleted, this));  
  6. void Sample_Class::onTimerServiceRequestCompleted( void *response)  
  7. {  
  8.     App42TimerResponse *app42TimerResponse = (App42TimerResponse*)response;  
  9.     if(app42TimerResponse->isSuccess)  
  10.     {  
  11.         printf("\ncode=%d",app42TimerResponse->getCode());  
  12.         printf("\nisSuccess=%d",app42TimerResponse->isSuccess);  
  13.         printf("\nResponse Body=%s",app42TimerResponse->getBody().c_str());  
  14.         printf("\n TimerName =%s",app42TimerResponse->app42Timer.name.c_str());  
  15.         printf("\n userName =%s",app42TimerResponse->app42Timer.userName.c_str());  
  16.         printf("\n startTime =%s",app42TimerResponse->app42Timer.startTime.c_str());  
  17.         printf("\n endTime =%s",app42TimerResponse->app42Timer.endTime.c_str());  
  18.         printf("\n isTimerActive=%d",app42TimerResponse->app42Timer.isTimerActive);  
  19.     }  
  20.     else  
  21.     {  
  22.         printf("\nerrordetails:%s",app42TimerResponse->errorDetails.c_str());  
  23.         printf("\nerrorMessage:%s",app42TimerResponse->errorMessage.c_str());  
  24.         printf("\nappErrorCode:%d",app42TimerResponse->appErrorCode);  
  25.         printf("\nhttpErrorCode:%d",app42TimerResponse->httpErrorCode);  
  26.     }  
  27. }  
Cancel Timer

Cancel the timer count down for a particular user.

Required Parameters

timerName - Name of the timer which has to be cancelled. userName - Name of the user for whom you want to cancel the timer count down.

  • create User Api for Android
  • create User Api for Windows
  • create User Api for iOS
  • create User Api for Swift
  • create User Api for Java
  • create User Api for Unity
  • create User Api for JS
  • create User Api for Corona
  • create User Api for Cocos2DX
  • create User Api for .Net
  • create User Api for PHP
  • create User Api for Marmalade
  •  create User Api for Ruby
  •  create User Api for Flash
  1. const char* timerName = "<Enter_your_timer_name>";  
  2. const char* userName = "John";  
  3. App42API::Initialize("API_KEY""SECRET_KEY");  
  4. TimerService *timerService = App42API::BuildTimerService();  
  5. timerService->CancelTimer(timerName, userName, app42callback(Sample_Class::onTimerServiceRequestCompleted, this));  
  6.   
  7. void Sample_Class::onTimerServiceRequestCompleted( void *response)  
  8. {  
  9.     App42TimerResponse *app42TimerResponse = (App42TimerResponse*)response;  
  10.     if(app42TimerResponse->isSuccess)  
  11.     {  
  12.         printf("\ncode=%d",app42TimerResponse->getCode());  
  13.         printf("\nisSuccess=%d",app42TimerResponse->isSuccess);  
  14.         printf("\nResponse Body=%s",app42TimerResponse->getBody().c_str());  
  15.         printf("\n TimerName =%s",app42TimerResponse->app42Timer.name.c_str());  
  16.         printf("\n userName =%s",app42TimerResponse->app42Timer.userName.c_str());  
  17.     }  
  18.     else  
  19.     {  
  20.         printf("\nerrordetails:%s",app42TimerResponse->errorDetails.c_str());  
  21.         printf("\nerrorMessage:%s",app42TimerResponse->errorMessage.c_str());  
  22.         printf("\nappErrorCode:%d",app42TimerResponse->appErrorCode);  
  23.         printf("\nhttpErrorCode:%d",app42TimerResponse->httpErrorCode);  
  24.     }  
  25. }  
Delete Timer

Delete a timer based on timer name.

Required Parameters

timerName - Name of the timer which has to be deleted.

  • create User Api for Android
  • create User Api for Windows
  • create User Api for iOS
  • create User Api for Swift
  • create User Api for Java
  • create User Api for Unity
  • create User Api for JS
  • create User Api for Corona
  • create User Api for Cocos2DX
  • create User Api for .Net
  • create User Api for PHP
  • create User Api for Marmalade
  •  create User Api for Ruby
  •  create User Api for Flash
  1. const char* timerName = "<Enter_your_timer_name>";  
  2. const char* userName = "John";  
  3. App42API::Initialize("API_KEY""SECRET_KEY");  
  4. TimerService *timerService = App42API::BuildTimerService();  
  5. timerService->DeleteTimer(timerName, app42callback(Sample_Class::onTimerServiceRequestCompleted, this));  
  6. void Sample_Class::onTimerServiceRequestCompleted( void *response)  
  7. {  
  8.     App42TimerResponse *app42TimerResponse = (App42TimerResponse*)response;  
  9.     if(app42TimerResponse->isSuccess)  
  10.     {  
  11.         printf("\ncode=%d",app42TimerResponse->getCode());  
  12.         printf("\nisSuccess=%d",app42TimerResponse->isSuccess);  
  13.         printf("\nResponse Body=%s",app42TimerResponse->getBody().c_str());  
  14.     }  
  15.     else  
  16.     {  
  17.         printf("\nerrordetails:%s",app42TimerResponse->errorDetails.c_str());  
  18.         printf("\nerrorMessage:%s",app42TimerResponse->errorMessage.c_str());  
  19.         printf("\nappErrorCode:%d",app42TimerResponse->appErrorCode);  
  20.         printf("\nhttpErrorCode:%d",app42TimerResponse->httpErrorCode);  
  21.     }  
  22. }  
Get Current Time

Fetch the current UTC time on server.

>
  • create User Api for Android
  • create User Api for Windows
  • create User Api for iOS
  • create User Api for Swift
  • create User Api for Java
  • create User Api for Unity
  • create User Api for JS
  • create User Api for Corona
  • create User Api for Cocos2DX
  • create User Api for .Net
  • create User Api for PHP
  • create User Api for Marmalade
  •  create User Api for Ruby
  •  create User Api for Flash
  1. const char* timerName = "<Enter_your_timer_name>";  
  2. const char* userName = "John";  
  3. App42API::Initialize("API_KEY""SECRET_KEY");  
  4. TimerService *timerService = App42API::BuildTimerService();  
  5. timerService->GetCurrentTime(app42callback(Sample_Class::onTimerServiceRequestCompleted, this));  
  6. void Sample_Class::onTimerServiceRequestCompleted( void *response)  
  7. {  
  8.     App42TimerResponse *app42TimerResponse = (App42TimerResponse*)response;  
  9.     if(app42TimerResponse->isSuccess)  
  10.     {  
  11.         printf("\ncode=%d",app42TimerResponse->getCode());  
  12.         printf("\nisSuccess=%d",app42TimerResponse->isSuccess);  
  13.         printf("\nResponse Body=%s",app42TimerResponse->getBody().c_str());  
  14.         printf("\n TimerName =%s",app42TimerResponse->app42Timer.name.c_str());  
  15.         printf("\n userName =%s",app42TimerResponse->app42Timer.userName.c_str());  
  16.         printf("\n startTime =%s",app42TimerResponse->app42Timer.startTime.c_str());  
  17.         printf("\n endTime =%s",app42TimerResponse->app42Timer.endTime.c_str());  
  18.         printf("\n isTimerActive=%d",app42TimerResponse->app42Timer.isTimerActive);  
  19.     }  
  20.     else  
  21.     {  
  22.         printf("\nerrordetails:%s",app42TimerResponse->errorDetails.c_str());  
  23.         printf("\nerrorMessage:%s",app42TimerResponse->errorMessage.c_str());  
  24.         printf("\nappErrorCode:%d",app42TimerResponse->appErrorCode);  
  25.         printf("\nhttpErrorCode:%d",app42TimerResponse->httpErrorCode);  
  26.     }  
  27. }  
Exception Handling

The functions available under Timer API can throw some exceptions in abnormal conditions. For example, if a developer is starting a time for user in app/game with a timer name which is not in the database, the function will throw the App42Exception (as shown below) with the message as “Not Found” and the appErrorCode as “5000” and the details as “Timer does not exist”.

  • create User Api for Android
  • create User Api for Windows
  • create User Api for iOS
  • create User Api for Swift
  • create User Api for Java
  • create User Api for Unity
  • create User Api for JS
  • create User Api for Corona
  • create User Api for Cocos2DX
  • create User Api for .Net
  • create User Api for PHP
  • create User Api for Marmalade
  •  create User Api for Ruby
  •  create User Api for Flash
Error Codes

Functions in Timer API might throw exceptions with following HTTP and Application Error Codes (along with their descriptions):

1400 - BAD REQUEST - The requested parameters are invalid. 1401 - UNAUTHORIZED - Client is not authorized. 1500 - INTERNAL SERVER ERROR - Internal Server Error. Please try again. 5000 - NOT FOUND - Timer does not exist. 5001 - BAD REQUEST - Timer already active. 5002 - BAD REQUEST - Timer already expired/cancelled.