Mix HTTP server and client #1620
-
Hi all, I am using the example of http server from Mongoose examples in a test project. Is it possible from this example to communicate with another server using an http client request? Can I use both at the same time in the main? Thanks in advance |
Beta Was this translation helpful? Give feedback.
Replies: 14 comments 3 replies
-
As long as you assign the proper function handler you can have several different connection types. In fact, the reverse proxy example does what you want. Tutorial here |
Beta Was this translation helpful? Give feedback.
-
Thank you for your response, I looked at the reverse proxy example. Can I use |
Beta Was this translation helpful? Give feedback.
-
Ok thank you, I am going to do some tests |
Beta Was this translation helpful? Give feedback.
-
Hello,
And myFn:
The mg_connect() seems to work properly because I see a connection on the server side. |
Beta Was this translation helpful? Give feedback.
-
ok thanks and I use the same |
Beta Was this translation helpful? Give feedback.
-
Hello,
but with |
Beta Was this translation helpful? Give feedback.
-
AFAIK there are no global variables in Mongoose if that is what you mean. |
Beta Was this translation helpful? Give feedback.
-
I agree with you that there are no global variables in Mongoose, I meant if I create an http listener in the main function and I want to create an http client in another part of the code (in a c++ object which is independant), can I use the same mgr? |
Beta Was this translation helpful? Give feedback.
-
As long as you keep everything in one thread you shouldn't have issues, but can you post a small example of your idea ? |
Beta Was this translation helpful? Give feedback.
-
Yes of course, here is a small simplified example of the idea: main.cpp:
HttpClient.h:
httpClient.cpp:
|
Beta Was this translation helpful? Give feedback.
-
Can't you just pass mgr as a parameter to the constructor and hold it as an object variable ? |
Beta Was this translation helpful? Give feedback.
-
Yes I could pass mgr as parameter, the function |
Beta Was this translation helpful? Give feedback.
-
From the docs
If you absolutely need multi-threading, take the time to go through the user guide (linked above) and tutorials, there is one that shows you how to do multi-threading. |
Beta Was this translation helpful? Give feedback.
-
Yes thank you, doc is well done, I understand and use the multi-threading from the event handler of mg_http_listen but in the case to create an mg_http_connect triggered by another thread , I don't see how to do it? Have you got a short example? |
Beta Was this translation helpful? Give feedback.
As long as you assign the proper function handler you can have several different connection types. In fact, the reverse proxy example does what you want. Tutorial here