Installation and test of libwebsockets [source]
- Download, configuration and installation:
sudo apt-get install autoconf libtool
git clone git://git.warmcat.com/libwebsockets
cd libwebsockets/
automake --add-missing
autoreconf
libtoolize
./configure --prefix=/usr
make
sudo make install
- Test:
libwebsockets-test-server
→ open your favourite browser and enjoy libwebsockets running on URL http://localhost:7681/.
Browser compatibility of WebSockets
While Chrome and Firefox showed the libwebsocket test page correctly, I had no luck with Opera (v12.02). This is caused by Operas restricted use of WebSockets. You can enable it by opening opera:config with Opera. Expand the list called “User Prefs” and check “Enable WebSockets”.
See caniuse.com for a list of different browser compatibilities regarding Web Sockets.
OpenFrameworks v0071 and ofxLibwebsockets
Download the code
- Open the addon folder of your OpenFrameworks installation
cd addons
- Download the addon for libwebsockets. With the main branch I had a segmentation fault in the end, that’s why I got the windows-fix branch recommended.
git clone -b windows-fix https://github.com/labatrockwell/ofxLibwebsockets.git
Compiling the ServerEcho example
- Copy the server example to the addons example folder and go to that location
cp -R ofxLibwebsockets/example-server-echo/ ../examples/addons/libwebsocketsServerEchoExample
cd ../examples/addons/libwebsocketsServerEchoExample/ - Generate the project files for your system
projectGenerator .
- When I compiled the project with “make”, it resulted in a bunch of errors:
/usr/include/c++/4.7/ctime:62:11: error: ‘::clock_t’ has not been declared
/usr/include/c++/4.7/ctime:63:11: error: ‘::time_t’ has not been declared
/usr/include/c++/4.7/ctime:64:11: error: ‘::tm’ has not been declared
This is happening because of a name clash, caused by a file in the addons “lib”-folder for Windows. I just deleted it to get rid of the error. There may be a better way to assure compilation under Windows will continue working..rm -R ../../../addons/ofxLibwebsockets/libs/libwebsockets/include/win32port/
- The next error is fixed in the above mentioned branch. Nevertheless, you could be faced with the following when running “make”:
src/testApp.cpp:89:23: error: conversion from ‘long int’ to ‘const Json::Value’ is ambiguous
To fix this, do the following:gedit ../../../addons/ofxLibwebsockets/libs/ofxLibwebsockets/src/Reactor.cpp &
- Change the lines 106 and 114 like this and save it:
//before:
//args.json = NULL;
args.json = Json::Value::null; gedit src/testApp.cpp &
- Change line 89 like this and save it:
//before:
//if ( args.json != NULL){
if ( args.json != Json::Value::null){
- Next error to cope with when running “make”:
../../../addons/ofxLibwebsockets/libs/libwebsockets/include/libwebsockets.h:425:20: error: ‘size_t’ has not been declared
gedit ../../../addons/ofxLibwebsockets/libs/libwebsockets/include/libwebsockets.h
- Insert at line 52:
#else
#include <poll.h>
#include <stdlib.h>
#endif
- My last errors were related to the missing compiled libwebsockets library for Linux 64bit:
Protocol.cpp:(.text+0x11f): undefined reference to `libwebsockets_broadcast'
Reactor.cpp:(.text+0xc5): undefined reference to `libwebsocket_context_destroy'
..
If you installed the library like described in the first part of this article, this is easy to fix.gedit config.make &
- Insert this line:
USER_LIBS = -lwebsockets
- Now you should be able to compile the example:
make
Testing the server
cd bin
./libwebsocketsServerEchoExample
Clicking anywhere on the window, it opens a website where you can test the service. In my case this worked in Chromium, but not in Firefox.
At first, the program was crushing:Compiled without SSL support, serving unencrypted
This is fixed by the above mentioned branch windows-fix.
Listening on port 9092
Created new window in existing browser session.
Segmentation fault (core dumped)
Testing the client
When I run the ClientHelloWorld example and connect it to “echo.websocket.org” by commenting in the regarding line in “src/testApp.cpp”, it’s not working:libwebsocket init success
client connection success
ERROR writing to socket
..
ERROR writing to socket
lws_client_interpret_server_handshake: NULL c_protocol
lws_client_interpret_server_handshake WSI_TOKEN_PROTOCOL is null
on close
The segmentation fault appears at line 690 in “libwebsockets/lib/libwebsockets.c”:
context->protocols[0].callback(context, wsi,
LWS_CALLBACK_CLEAR_MODE_POLL_FD,
(void *)(long)wsi->sock, NULL, POLLOUT);
Comments