Picture of Wim 4127
Registered 6 years 309 days
Wim 4127 Sunday, 26 September 2021, 05:49 PM
Create custom API in C++ backend
Is it possible to create a custom JSON API in a C++ backend that will handle a specific set of JSON messages from the front-end?
We tried by setting the "api" property in the JSON message from the front-end to a certain name. This is the name that is returned by my C++ API class that is derived from JsonApi. However, this class doesn't get the messages in it's Message function that is overridden.

So is this possible and if so, when should I register the API class in the session. I tried in the AppWebsocketConnectComplete overridden function of the session, but I don't see any message coming in.

Can you please provide an example if possible?

Andreas Fink
Moderator Registered 13 years 109 days
Andreas Fink (innovaphone) Thursday, 30 September 2021, 06:43 PM
Re: Create custom API in C++ backend
Hello Wim,

I have attached an implementation, based on the documentation here:
https://sdk.innovaphone.com/13r2/sdk/common/interface/json_api.htm

You have to integrate it in your project, e.g. boilerplate app you get on a freshly created C++ App project:
  • #include "jsonapiexample_echo.h"
  • don't forget to add it to make file
Extend header file of the app class by the member variable for the context:
class ITask * currentTask;
std::list<class jsonapiexampleSession *> sessionList;
class EchoJsonApiContext * echoJsonApiContext;


Instantiate the Context object right after RegisterJsonApi(this) for default AppWebsocket in the app class constructor implementation:

RegisterJsonApi(this);

this->echoJsonApiContext = new EchoJsonApiContext("EchoApi", this);

Log("App instance started");

Make sure to release it in the destructor:
jsonapiexample::~jsonapiexample()
{
delete this->echoJsonApiContext;
}

The JavaScript client implementation can use the custom API directly the AppWebsocket connection:

function app_connected(domain, user, dn, appdomain) {
var src = new app.Src(update);
src.send({ mt: "MonitorCount" });
src.send({ api:"EchoApi", mt: "Ping", text: "foobar" });

Best Regards
Andreas Fink



jsonapiexample.zip
← You can define your color theme preference here