开发者

communication between javascript and C++ code through web sockets?

I have javascript (client - executed through node.js) and C++ (server) code running on Ubuntu (Linux) and I want this client-server to communicate with each other. Can somebody tell me how I can make C++ code work like a server or client using web socket? Basically, I want javascript code to send some data to C++ code, the C++ code will process on the data and return the result back to javascript code. I'm not sure if I this communication between javascript and C++ code can happen with out web socket. Any point开发者_开发技巧ers in this direction would be of great help!

Thanks, pats


I very recently started working on a C++ websocket library: https://github.com/szmoore/foxbox

An example of a websocket server is: https://github.com/szmoore/foxbox/blob/master/examples/wsserver.cpp

I also have an example of a JavaScript client.

Warnings: The library doesn't support TLS, is based around POSIX sockets, and is still in development and probably horribly insecure.

So, whilst shamelessly promoting my own library I will also point you at libwebsockets, a C library suggested in answer to this question: https://stackoverflow.com/questions/3916217/standalone-c-or-c-websocket-server-library?lq=1


You have several choices. I am assuming your C++ server already has a websocket server running on it but if not, get Mongoose or the non-GPL fork Civetweb. Both are tiny bits of c code you build into your C++ program to add webserver,including websockets, functionality.

In fact, civetweb comes with a websocket example.

However, you do not need a websocket, just an ordinary socket should do. You'll probably want to send the data in JSON format to makeit easy to consume by the javascript code.


I know this question was asked a LOOONG time ago, but here is a gist of my set up that allows to have a communication between my c++ and Javascript apps; if anyone stumbles on this question:

  1. I use TCP/IP connections to set up communication between a C++ app and a Javascript (Typescript) app. ZeroMQ (0mq) library is perfect for that. On c++ side you have libs like zmqpp and on the JS side you have zeromq.js.
  2. After the data is received in JS land, you can use Socket.IO if you would like to have that streaming data available in a browser. Generally, you would have to forward the data stream off 0mq through Socket.IO and then access it on the browser side.
  3. Bonus: make the data format using Protobuf.


On the Javascript side you will probably want to use an XmlHttpRequest. This will cause the javascript to post an HTTP(s) request to your server. For the C++ server side, you can look at something like Pion for an embeddable HTTP server or if you want to link into a full web server like Apache, you can use Fast CGI to plug your server code into Apache.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜