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 installationcd 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 locationcp -R ofxLibwebsockets/example-server-echo/ ../examples/addons/libwebsocketsServerEchoExamplecd ../examples/addons/libwebsocketsServerEchoExample/

Generate the project files for your systemprojectGenerator .

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 ambiguousTo fix this, do the following:

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
Listening on port 9092
Created new window in existing browser session.
Segmentation fault (core dumped)This is fixed by the above mentioned branch windows-fix.

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);