[Libwebsockets] libwebsockets from C++

"Andy Green (林安廸)" andy at warmcat.com
Fri Feb 1 01:33:03 CET 2013


On 01/02/13 01:50, the mail apparently from Drew Noakes included:

(looping the libwebsockets mailing list)

> Thanks for your work on libwebsockets. It looks like a very well put
> together open source project and it's great to see such activity on it
> over the week or so that I've been looking at it.
>
> It's been a long time since I've done any C, and my C++ is passable but
> not great (I'm much stronger in managed languages like Java/C#, and my
> native skills have atrophied). I'm having trouble building a test server
> using g++ and I'm sure it's something really obvious.
>
> I've created a new repo and added 'libwebsockets' as a submodule. In my
> root I have a modified version of test-server.c that I've started to
> convert into C++ as a first step towards integrating it into my
> application (which runs on a small humanoid robot as part of a
> university RoboCup team.) I'd like to stream real-time telemetry to a
> browser app using your library.
>
> I run a few open source projects myself and know that it's hard to field
> questions from people without much experience, so rather than ask about
> my specific compiler problems (undefined references), I wonder if you
> would be able to put together some sample code that demonstrated an
> advisable way of creating a libwebsockets-based service within a C++
> application, as well as perhaps a simple makefile.
>
> Selfishly, I am not interested in daemonising, SSL or client code.
> Others may be.
>
> Thanks again for your efforts on this open source project.

The reason there's no g++ specific sample is it's not any big deal.

To reproduce a workable result, copy the test-server.c somewhere.

C++ has slightly different rules, void * is not magic casting glue any 
more, so you need this cosmetic patch to get rid of casting complaints

--- a/test-server.c	2013-01-31 09:39:09.794712848 +0800
+++ b/test.cpp	2013-02-01 08:25:27.533450384 +0800
@@ -107,7 +107,7 @@
  	case LWS_CALLBACK_HTTP:

  		for (n = 0; n < (sizeof(whitelist) / sizeof(whitelist[0]) - 1); n++)
-			if (in && strcmp(in, whitelist[n].urlpath) == 0)
+			if (in && strcmp((const char *)in, whitelist[n].urlpath) == 0)
  				break;

  		sprintf(buf, LOCAL_RESOURCE_PATH"%s", whitelist[n].urlpath);
@@ -260,7 +260,7 @@
  	unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 +
  						  LWS_SEND_BUFFER_POST_PADDING];
  	unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
-	struct per_session_data__dumb_increment *pss = user;
+	struct per_session_data__dumb_increment *pss = (struct 
per_session_data__dumb_increment *)user;

  	switch (reason) {

@@ -288,7 +288,7 @@
  //		fprintf(stderr, "rx %d\n", (int)len);
  		if (len < 6)
  			break;
-		if (strcmp(in, "reset\n") == 0)
+		if (strcmp((const char *)in, "reset\n") == 0)
  			pss->number = 0;
  		break;
  	/*
@@ -337,7 +337,7 @@
  					       void *user, void *in, size_t len)
  {
  	int n;
-	struct per_session_data__lws_mirror *pss = user;
+	struct per_session_data__lws_mirror *pss = (struct 
per_session_data__lws_mirror *)user;

  	switch (reason) {


Afterwards, you can build it as "cpptest" just like this

g++ -DINSTALL_DATADIR=\"/usr/share\" -ocpptest test.cpp -lwebsockets

You only need to care about INSTALL_DATADIR because the test-server 
wants it, if your version based on that doesn't care you won't need to 
define it either.

-Andy




More information about the Libwebsockets mailing list