Many games require the use of http requests in order to communicate with external servers. This could be for pulling down the latest game assets, updating leaderboards or grabbing a users save data. This is achieved in Chilli Source using the HttpRequestSystem. The HttpRequestSystem can be used to send GET and POST requests.
The HttpRequestSystem is an optional app system and must therefore be created in the derived application class:
#include
void MyApp::CreateSystems()
{
CreateSystem();
}
As with any app system it can be retrieved via application:
auto httpSystem = CSCore::Application::Get()->GetSystem();
Once we have the system we can make http requests. This example will show a simple GET request:
httpRequestSystem->MakeGetRequest(“www.myurl.com”, [](const CSNetworking::HttpRequest* in_request, const CSNetworking::HttpResponse& in_response)
{
switch (in_response.GetResult())
{
case CSNetworking::HttpResponse::Result::k_completed:
CS_LOG_VERBOSE("HTTP Completed: " + in_response.GetDataAsString());
break;
case CSNetworking::HttpResponse::Result::k_failed:
CS_LOG_VERBOSE("HTTP Failed");
break;
case CSNetworking::HttpResponse::Result::k_timeout:
CS_LOG_VERBOSE("HTTP Timeout (URL: " + in_request->GetUrl() + ")");
break;
}
});
When the request completes the delegate is called with the original request (containing the url, headers, etc.) and a response containing the result, the response code and the response data.
First the result must be checked:
- Completed: The request was successful and the response code and data are available.
- Failed: The request failed and no data is available.
- Timeout: A connection to the remote location was not established in time (this can be configured when making the request).
If the result is completed then the data and http code can be accessed via the response.
That’s pretty much it. The only other concept to mention is canceling a request (which you may wish to do if you no longer care about the response). Requests are canceled like this:
CSNetworking::HttpRequest* request = httpRequestSystem->MakeGetRequest(“www.myurl.com”, …);
request->Cancel();
The request is returned from the make call and is used to cancel.