question

Yue Y avatar image
0 Likes"
Yue Y asked Phil BoBo edited

How to make asynchronous call to web service

Hi, I am considering using a web service inside FlexSim. After reading https://answers.flexsim.com/questions/21092/web-communication-within-flexsim.html. I know FlexSim is capable of sending HTTP request and response. But I don't want FlexSim to be blocked on the HTTP call, instead, my goal is to let the simulation continue and react to the response asynchronously. For example, say there a source creating 1 token every one second, the token is sent to a processor. The logic inside the processor is, sending a request to a remote web service and the token should leave the processor after receiving a response. While waiting for the response, FlexSim shouldn't freeze. The source should continue emitting tokens and the process should keep accepting tokens.

I am thinking about using a background thread listing to responses and updating tokens' state accordingly, the processor will react to the tokens' state.

Is the possible?

FlexSim 19.1.0
custom codewebserverhttpweb communicationsynchronization
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Jacob Gillespie avatar image
3 Likes"
Jacob Gillespie answered Phil BoBo edited

An asynchronous call could be achieved by letting an html GUI element handle it. I am thinking you could create a hidden html view and use callwebscriptmethod() to start the request, and then use fireFlexscriptEvent() from Javascript to get the response back to FlexSim.

Here is a sample model with both of those methods.


· 8
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jacob Gillespie avatar image Jacob Gillespie ♦ commented ·

@Yue Y

There is also this command:

applicationcommand("downloadhttprequest", verb, server, object, data, silent, filePath, callbackNode);

It should work similarly to the command in the link you included above. The result of the request is saved to the filePath supplied.

I haven't tested it but I believe it works asynchronously.

2 Likes 2 ·
Phil BoBo avatar image Phil BoBo ♦♦ Jacob Gillespie ♦ commented ·

That application command does work for asynchronous http requests.

Here is some example code from a model I built that used that command to make asynchronous calls to the FlexSim webserver:

Starting the request:

string server = ServerAddress;
string instancenum = string.fromNum(InstanceNum,0);
string modelname = currentfile();
int index = modelname.lastIndexOf("\\");
modelname = modelname.substr(index + 1, modelname.length - index - 4);
string object = "/webserver.dll?queryinstance=" + modelname + "&instancenum=" + instancenum + "&executeRPC";
string data = "command=" + command;
int silent = 0;
string filePath = modeldir() + "ClientSendRPC.txt";
treenode callback = model().find("Tools/ClientSendRPC");
applicationcommand("downloadhttprequest", "POST", server, object, data, silent, filePath, callback);

Handling the request (callback node code):

int status = param(1);
double sizeCompleted = param(2);
double sizeTotal = param(3);
#define WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING 0x00000800
if (status == WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING) {
	if (fileopen("ClientSendRPC.txt", "r")) {
		print("Read complete, ", sizeCompleted, " bytes: ");
		string line = filereadline();
		while (line.length > 0) {
			print(line);
			line = filereadline();
		}
		fileclose();
	}
}

And here's a simple example that gets the www.flexsim.com page:

downloadhttprequest-example.fsm

1 Like 1 ·
Yue Y avatar image Yue Y commented ·

Hi Jacob,

thank you for your advice! Is it possible to send a request from FlexSim without waiting for a response? Instead, I want to have a separate thread processing all incoming requests one and a time.

0 Likes 0 ·
Jacob Gillespie avatar image Jacob Gillespie ♦ Yue Y commented ·

@Yue Y The "downloadhttprequest" I mentioned above doesn't wait for a response. Instead it saves the result to the specified file and then calls the callbackNode from which you can handle the response as in the example given by Phil.

1 Like 1 ·
Yue Y avatar image Yue Y Jacob Gillespie ♦ commented ·

Thank you! Does it imply that this command won't block? What if the response never arrives, how would it affect the behavior of this command?

0 Likes 0 ·
Show more comments
Yue Y avatar image Yue Y commented ·

Is there any restriction on the payload of http request and response in FlexSim?

eg, the maximum size of body

0 Likes 0 ·
Jacob Gillespie avatar image Jacob Gillespie ♦ Yue Y commented ·

I don't think there is a size limit.

0 Likes 0 ·