Reputation: 43
What I want to achieve is to have a webpage send a message to a C++ application, but this requires some form of connection from the C++ to the web server, and from the web server to the client.
One way I thought of doing it would be to have a MySQL database which stores a message, and the C++ app uses libCURL to ask the web server via a php script for any new messages. It would have to poll it every second or so, which would be a bit of a waste. In addition, whenever you wanted to send a message from the webpage, you'd have to submit the form back to the web server, which would be slow.
I was also looking at HTML5 websockets, thinking that it might be possible to create a TCP connection between the client and the C++ app, but I don't think that's how they work. Are websockets designed to route messages between TWO HTML5 applications via the server, or could you theoretically use a HTML5 application to talk directly to the server (maybe run a php script, or send a normal TCP packet)?
Any suggestions for how I could achieve this would be welcomed.
Thanks.
Upvotes: 4
Views: 2382
Reputation: 1534
This is not very easy. You should look at Google's solution to this:
Called Cloud to Device Messaging Framework used for sending message to Android devices.
What you are doing is called Push. Here is a video on this from Google IO.
Just think of your c++ application as the Android Device.
Your C++ app would make a connection to the server over some tcp/ip port to a service you create that just waits for connections. Your app would tell the server the ID of the user running the app or if it was multicast your app would just wait for the sevice to give it a message.
Your webserver can use interprocess comunication, namedpipes, named mutexes, etc to single your service to send a message down all the open TCP/IP connections.
Upvotes: 1
Reputation: 88155
If you're writing the C++ program you can just write it to make use of CGI so that your web server will run your C++ program as a 'script' to handle the relevant http requests and you will directly output the http response from your C++ program.
http://en.wikipedia.org/wiki/Common_Gateway_Interface
Upvotes: 2
Reputation: 8975
If you want to do remote procedure calls from your website, I'd recommend taking a look at SOAP. SOAP works perfectly with PHP, and with gSOAP you can easily embed a server interface into your C++ application, or vice versa if necessary.
If you have only very simple messages to bring across your application, your database idea would work as well, but your application will have to keep on checking whether there is a new message available (which means a lot of unnecessary traffic, at least if your database is not on the same machine as your application), and you will have to deal with these checks yourself, come up with models, etc.
SOAP on the other hand sends your data directly to your server application, and handles all the nasty stuff like type safety or serialization implicitly.
You can find a lot of documentation and examples, neatly explaining how SOAP is defined and how it works from the World Wide Web Consortium. There are also various examples of how to set up your SOAP server in C++ in gSOAP's User Guide.
Upvotes: 3