WebSocket implementation in oF

Hi everyone!

I’m currently working on getting ofxNetwork’s TCP server to talk to HTML5 websockets. In theory it should be easy but of course there’s a ridiculously convoluted handshake that you need to implement for a successful connection.

I’m getting close, but I just wondered if anyone has already done this? It could save me some time :slight_smile: Naturally, I’ll share my solution when I get there.

I’ve also found this libwebsockets library but I’m not sure how I’d set that up in oF. http://git.warmcat.com/cgi-bin/cgit/libwebsockets/ Any thoughts?

cheers!

Seb

Seb, any luck in getting OF websocket communication to work? I’m looking for similar capabilities and don’t want to reinvent the wheel.

I know the handshake is a pain, but it’s pretty transparently implemented in some other places. Let me know if some pointers would be helpful.

.aaaron

played with libwebsockets a bit last night. I was able to compile it for the Mac and get the test.c server running in XCode and was to connect a browser to it.

Steps were this:

downloaded libwebsockets
http://git.warmcat.com/cgi-bin/cgit/libwebsockets/snapshot/support-protocol-v7.tar.gz

used this option from README-test-server to compile for the mac

  
./configure CC="gcc -arch i386 -arch x86_64" CXX="g++ -arch i386 -arch  
x86_64" CPP="gcc -E" CXXCPP="g++ -E" --enable-nofork  
  

did the rest of the install

  
$ make clean  
$ make  
$ sudo make install  

added to the Xcode project

  
libwebsockets.h  
libwebsockets.c  
libwebsockets.0.dylib  
libwebsockets.a  
libwebsockets.dylib  

added
test-server.c to Xcode
remove main.cpp, testApp.cpp from the OF target

added the lines to test.c

  
  
//this is from support-protocol-v7/test-server, test.html is in here  
#define DATADIR "/Volumes/WORK_IN_PROGRESS/SERVER/websocketstest/"  
#define LOCAL_RESOURCE_PATH "/Volumes/WORK_IN_PROGRESS/SERVER/websocketstest/"  
#define LWS_NO_FORK 1  
  

at this point I believe it would just be transferring the C code over to an OF app. I may mess around with it a bit today to see if I can get ahead on that

Hi guys,

yeah it was a pain but I got the handshake working with oF’s socket server in the end (with the help of my friend Paul King twitter@nrocy). It’s a bit messy but it works! I’m running my courses in NYC this week so I probably won’t be able to post anything until next week, but Aaron, let me know if you need it before then and I’ll send you the files.

Jvcleave - that’s really useful thank you so much for writing up the process, I may well give that a go too.

cheers!

Seb

messing around with it a bit more I got libwebsockets to work as a static lib so the install is no longer necessary. I plan on putting it up on github as soon as I get connection working from c++ (getting errors) but if anyone wants the Xcode project as is (test-server.c works), hit me up

just ran into Paul Reimer’s github which looks pretty far if you are using 007

https://github.com/paulreimer

wow, that does look good! OK will look into that when I return to this project next week. Thank you!

Seb

Hi,
Just wanted to share how I got libwebsockets working with OF on Ubuntu/Linux. Took a lot of trial and error but it’s actually pretty simple in the end.

  1. [font=monospace]git clone git://git.warmcat.com/libwebsockets[/font] into a directory of choice.
  2. [font=monospace]./configure; make; sudo make install;[/font] in the libwebsockets directory to build and install.
  3. [font=monospace]sudo cp /usr/local/lib/libwebsockets.so.0 /usr/lib/[/font] to move the library into a place where OF can find it (this one took me a while… but maybe there’s a better way?)
  4. Edit config.make in your OF project directory. Add [font=monospace]-I[wherever_you_cloned_libwebsockets]/lib[/font] to USER_CFLAGS so it can find the header files (libwebsockets.h) and compile correctly. Add [font=monospace]-lwebsockets[/font] to USER_LIBS so it can link to the library.
  5. Edit libwebsockets.h. Move the [font=monospace]extern C {[/font] and closing [font=monospace]}[/font] OUTSIDE of the [font=monospace]#ifdef WIN32[/font] blocks. I don’t know why this wasn’t done before… seems like the extern blocks are needed to include this c library in any c++ project.

Hope that helps someone!

Just noticed one other change I had to make stemming from a difference between c and c++:
The example server callback functions take (void *) in for the user data pointer and a pointer to the incoming data string. These are implicitly cast to a [font=monospace]struct per_session_data__dumb_increment *[/font] and [font=monospace]const char *[/font] except C++ doesn’t allow [font=monospace]void *[/font] to be implicitly cast so you must make it explicit, something like:

  
const char *in = (const char *)_in;  

Nothing to contribute yet - just saw Seb demo his Phone Pixels project for the second time at Flash On The Beach ( http://vimeo.com/28984329 ) and thought I’d search the forums for info on using Web Sockets. Of course this is the first thread I find…

I just tried jvcleave’s 0062 code on 10.7 with latest Chrome and Firefox. It turns out they require the latest v8 version of Websockets, which libwebsockets already supports here:

http://git.warmcat.com/cgi-bin/cgit/libwebsockets/snapshot/support-protocol-v8-chrome-15-firefox-6.tar.gz

I followed jvcleave’s instructions for v7, and everything still worked fine.

Running the example server in:

/support-protocol-v8-chrome-15-firefox-6/test-server

Meant that I could go to:

http://localhost:7681/

And run the drawing and counter tests.

I’m now working on getting jvcleave’s code working with v8. Then I’m going to go through and get everything working with 007. I tried Paul Reimer’s github code, but found it quite confusing, so I am going to stick with building up from the 0062 system.

Hey Seb,
Where did you end up at with your oF solution? I’m close, but seem stuck on correctly delivering the handshake to the client; seems like you were perhaps in the same position.

(also looking into jvcleave’s ofxLibWebsockets…some good stuff there for sure!)

Just noticed this reply, sorry! I got the handshake working for the older protocol (with the help of Paul King) but with some fairly nasty hacking of ofxTCPServer. I’ve been talking to Roxlu who was doing some work on getting one of the other C libs implemented as an addon, I haven’t chatted to him for a while so not sure where he’s at with that.

cheers!

Seb

Anyone know if there’s been an OF websocket kit that has become the defacto kit? thanks

i would go for this one
https://github.com/labatrockwell/ofxLibwebsockets

thanks bud