-4

I am required to implement a websocket server on C++ code running on Linux. At times, I also need to push data asynchronously to client.

So far, I have tried beast echo server, libwebsocket test server and libonion (It would crash once attempted to connect from a browser with error "Cant calculate SHA1 if gnutls is not compiled in! Aborting now". But, I haven't been able to come up with anything that I can build upon.

Below I am trying to provide more information by dummy test class.

test.cpp
class test
{
public:
void processDataFromClient(const string & data);//this needs to get filled by websocket read method? how?
void pushDataToClient(const string & data);

};
test::pushDataToClient(const string & data)
{
(socketId, text) // how do I call this from my test.cpp ?
}

test::void processDataFromClient(const string & data)
{
//do some computation based on the text received
//turn on the light
//once Light is on for a minute call
pushDataToClient( lighton) ;
}

//websocket_server.cpp 
main
{
//open a webSocket
//listen for messages from client i.e pass data received from onion_websocket_read to test class by calling processDataFromClient();
//send asynchronous messages to client received from test class. i.e. pushDataToClient should invoke onion_websocket_write //do we need a thread for this ? how to achieve this?

}

I am looking for a simple interface that would allow me to start a websocket, receive and send data to/from client asynchronously.

user1867459
  • 423
  • 2
  • 8
  • 27

2 Answers2

0

I am required to implement a server on C++ code running on Linux to support Angular JS client. What are the options available ? Shall I use c++ to implement server over raw socket, boost-asio server, CGI based server support, REST server or is there any better/faster option. I have never implemented this before and unaware of the drawback/advantages of each of the options. I have never coded CGI and REST api before. Let me know your comments.

Perhaps I misunderstand your question, but are you talking about rewriting some kind of socket-based Apache (or nodejs) in C++? AngularJS is a framework that runs in-browser. When you say "Support Angular JS client," you mean serving HTML/HTTP-based applications with C++? This would encompass other frameworks and JavaScript-based implementations.

If this is the requirement, I wouldn't do this from scratch. It's a massive undertaking. There are projects out there that you can work with and build upon.

Here's a couple of references:

https://softwareengineering.stackexchange.com/questions/53624/can-c-be-used-as-a-server-side-web-development-language How to use C++ for apache server

EDIT: Based on comments below, perhaps one of these solutions? They are modules that enable you to call C++ programs from Node/Apache:

How can I use a C++ library from node.js?
Forwarding apache request to a c++ program

Community
  • 1
  • 1
kmiklas
  • 13,085
  • 22
  • 67
  • 103
  • I have a GUI/Client which is developed with AngularJS which will send HTTP request to the C++ code running on Linux. I need to parse those request and send response to the client. I quickly looked upon the links you sent but it is also very overwhelming to me since it doesn't specify a particular solution. I think you are suggesting a C++ based server framework where I don't have to start from scratch. if so , could you please suggest the steps with more informatio? – user1867459 Jan 23 '17 at 17:45
  • I suggested a C++ server framework, because that what you asked for. Given that you're working with Angular, you must know some JavaScript. Perhaps first look at Nodejs, and see if you can write the C++ pieces to plug in? Of course, there's Apache, which is Java. – kmiklas Jan 23 '17 at 18:06
  • I came across this C++ framework. https://github.com/corvusoft/restbed Not sure, if I should use this or not. Please let me know your comments. – user1867459 Jan 23 '17 at 18:12
0

You first need to understand deeply HTTP, WebSockets, and the close relation between both. Spend several weeks to study both. You'll probably need to understand also HTML5 & JavaScript & AJAX.

(so you might need to start by spending a few months learning more about all these subjects; you can find several books and websites related to them.)

Better use some C and C++ HTTP server library providing also WebSocket support. Like e.g. libonion (and perhaps Wt, but I am not sure it supports WebSockets.). libonion has an example for websocket (of course you need a very recent snapshot).

After spending some time (perhaps months, a few days are not enough) to understand well HTTP & WebSockets you are likely to realize that you should implement a C++ server program handling both HTTP & WebSockets. Just having a C++ program "simply" and only doing WebSockets -without handling HTTP- is probably meaningless, and certainly useless. But your specialized HTTP+WebSocket server program in C++ could get some requests forwarded from a general purpose Web server (read about proxy servers).

See also the Comparison of WebSockets implementations wikipage.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547