WebSocket Server Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

WebSocket Server

Post by Natulux »

Hey.

I used to use wxSocketBase to host a local server and (maybe) misused it even as HTTP server. Now I want to add a WebSocket server functionality, but I am not sure if the wxSockets are suited for this case. My client will be javascript from a 'webside', which my C++/wxWidgets program will host in an embedded browser. The purpose is communication between web page and server. The page can always fire an http request, but the server does not have this possiblity. That is why I wanted to try websockets.
To my understanding they are basically like http request/response, but the connection, once established, is not closed after the handshake and kept open for further request / response scenario + the server can send requests to the client. This means either event based handling on the server side (wxSocketBase) or a seperate thread.

Do you know about a WebSocket - Server implementation for C++/wxWidgets (msw)? Do you think, I could use wxSocketBase easily to implement a (local) webSocket server? I would like to just enhance my pseudo server (sindce it basically is also http/tcp) but my first concern is, how to seperate WebSocket request from HTTP requests. The scheme would help (http://localhost vs. ws://localhost) but I have not found any way to get the scheme from any incoming wxSocketBase request yet.

Thanks
Natulux
User avatar
T-Rex
Moderator
Moderator
Posts: 1248
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: WebSocket Server

Post by T-Rex »

Just use libwebsockets in your application. You should be able to build it on all popular platforms, as a 3rd-party library which will be used for your app.
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

Re: WebSocket Server

Post by Natulux »

T-Rex wrote: Tue Oct 08, 2019 3:05 pm Just use libwebsockets in your application. You should be able to build it on all popular platforms, as a 3rd-party library which will be used for your app.
Thank you for the hint!
I'll have a look into it. I hope I can include this in my static build, but on first glance it provides everything I need.

Cheers
Natu
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

Re: WebSocket Server

Post by Natulux »

Libwebsocket might be more powerfull, but it lets you know every now and then that it is made by and for linux users and I had a lot of frustration trying to use it on my windows machine in a C++ project.

If you are searching for a websocket lib and you are using C++ you might consider using "Simple-WebSocket-Server" like I did.
https://gitlab.com/eidheim/Simple-WebSocket-Server
It is a quite elegant way of having a C++ WebSocket-Server, using modern C++ methods.

If you do, there is one major hurdle, though: The Simple WebServer uses asio(standalone) or boost/asio. Asio on the other hand depends on including WinSock2, which can interfere with wxWidgets. You need to include wxWidgets classes first, before you include the WebServer classes.

Code: Select all

//first:
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif
//second: (in your websocketserver class
#include "server_ws.hpp"
//do not include wx/wx.h after this (e.g. in a sub class)
And I also found, that I need these preprocessor values for asio with wxWidgets:
//If using standalone asio and not boost/asio:
USE_STANDALONE_ASIO
//either case
WX_LEAN_AND_MEAN
WIN32_LEAN_AND_MEAN
Cheers
Natu
Post Reply