Support Forum       G3D Web Page     
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
G3D::WebServer Class Reference


Web server with support for https and websockets More...

Inherits G3D::ReferenceCountedObject.

Classes

class  SocketScheme
 
class  Specification
 
class  WebSocket
 Subclass this if your websockets do not need to share state across all instances of a URI handler per-WebServer instance. More...
 

Public Types

typedef std::function< shared_ptr< WebSocket >WebServer *, mg_connection *, const NetAddress &)> SocketFactory
 

Public Member Functions

void getWebSocketArray (const String &uri, Array< shared_ptr< WebSocket >> &array)
 Get all web sockets that are currently open and responding to this uri. More...
 
void registerWebSocketHandler (const String &uri, SocketFactory socketFactory)
 Register a set of event handlers for a specific websocket URI (e.g., "/websocket", "/lobby", "/game"). More...
 
void start ()
 
void stop ()
 The destructor automatically invokes stop(). More...
 

Static Public Member Functions

static shared_ptr< WebServercreate (const Specification &specification=Specification())
 The server is not started on creation. More...
 

Static Public Attributes

static const uint16 DEFAULT_PORT = 8080
 

Protected Member Functions

 WebServer (const Specification &)
 
virtual ~WebServer ()
 
virtual void onWebSocketClose (const shared_ptr< WebSocket > &socket)
 
virtual bool onWebSocketConnect (const shared_ptr< WebSocket > &socket)
 
virtual bool onWebSocketData (const shared_ptr< WebSocket > &socket, WebSocket::Opcode opcode, char *data, size_t data_len)
 
virtual void onWebSocketReady (const shared_ptr< WebSocket > &socket)
 Default implementation invokes socket->onReady. More...
 
shared_ptr< WebSocketsocketFromConnection (mg_connection *connection) const
 

Static Protected Member Functions

template<class T , class ... ArgTypes>
static shared_ptr< T > createShared (ArgTypes &&... args)
 Like std::make_shared, but works for protected constructors. More...
 
static void websocket_close_handler (const struct mg_connection *conn, void *userdata)
 
static int websocket_connect_handler (const struct mg_connection *conn, void *user_data)
 
static int websocket_data_handler (struct mg_connection *conn, int flags, char *data, size_t data_len, void *user_data)
 
static void websocket_ready_handler (struct mg_connection *conn, void *user_data)
 

Protected Attributes

mg_context * m_context
 
Table< String, SocketScheme * > m_socketSchemeTable
 Maps URI schemes to SocketSchemes. More...
 
Table< mg_connection *, shared_ptr< WebSocket > > m_socketTable
 Protected by m_socketTableMutex. More...
 
std::mutex m_socketTableMutex
 
Specification m_specification
 

Detailed Description


Web server with support for https and websockets

Member Typedef Documentation

◆ SocketFactory

typedef std::function<shared_ptr<WebSocket>WebServer*, mg_connection*, const NetAddress&)> G3D::WebServer::SocketFactory

Constructor & Destructor Documentation

◆ WebServer()

G3D::WebServer::WebServer ( const Specification )
protected

◆ ~WebServer()

virtual G3D::WebServer::~WebServer ( )
protectedvirtual

Member Function Documentation

◆ create()

static shared_ptr<WebServer> G3D::WebServer::create ( const Specification specification = Specification())
static

The server is not started on creation.

Invoke start().

◆ createShared()

template<class T , class ... ArgTypes>
static shared_ptr<T> G3D::ReferenceCountedObject::createShared ( ArgTypes &&...  args)
inlinestaticprotectedinherited

Like std::make_shared, but works for protected constructors.

Call as createShared<myclass>.

◆ getWebSocketArray()

void G3D::WebServer::getWebSocketArray ( const String uri,
Array< shared_ptr< WebSocket >> &  array 
)

Get all web sockets that are currently open and responding to this uri.

Useful for iterating through clients. Threadsafe.

Parameters
uriThe uri parameter passed to registerWebSocketHandler to facilitate the creation of these sockets.

◆ onWebSocketClose()

virtual void G3D::WebServer::onWebSocketClose ( const shared_ptr< WebSocket > &  socket)
inlineprotectedvirtual

◆ onWebSocketConnect()

virtual bool G3D::WebServer::onWebSocketConnect ( const shared_ptr< WebSocket > &  socket)
inlineprotectedvirtual

◆ onWebSocketData()

virtual bool G3D::WebServer::onWebSocketData ( const shared_ptr< WebSocket > &  socket,
WebSocket::Opcode  opcode,
char *  data,
size_t  data_len 
)
inlineprotectedvirtual

◆ onWebSocketReady()

virtual void G3D::WebServer::onWebSocketReady ( const shared_ptr< WebSocket > &  socket)
inlineprotectedvirtual

Default implementation invokes socket->onReady.

◆ registerWebSocketHandler()

void G3D::WebServer::registerWebSocketHandler ( const String uri,
SocketFactory  socketFactory 
)

Register a set of event handlers for a specific websocket URI (e.g., "/websocket", "/lobby", "/game").

Example:

class MySocket : public WebServer::WebSocket { protected: MySocket(WebServer* server, const NetAddress& clientAddress) : WebSocket(server, clientAddress) {} public: static shared_ptr<WebSocket> create(WebServer* server, const NetAddress& clientAddress) { return createShared<MySocket>(server, clientAddress); }

bool onData(int flags, char* data, size_t data_len) override { printf("Received data: %s\n", data); return true; } };

server->registerWebSocketHandler("/print", &MySocket::create);

See also
getWebSocketArray

◆ socketFromConnection()

shared_ptr<WebSocket> G3D::WebServer::socketFromConnection ( mg_connection *  connection) const
protected

◆ start()

void G3D::WebServer::start ( )

◆ stop()

void G3D::WebServer::stop ( )

The destructor automatically invokes stop().

◆ websocket_close_handler()

static void G3D::WebServer::websocket_close_handler ( const struct mg_connection *  conn,
void *  userdata 
)
staticprotected

◆ websocket_connect_handler()

static int G3D::WebServer::websocket_connect_handler ( const struct mg_connection *  conn,
void *  user_data 
)
staticprotected

◆ websocket_data_handler()

static int G3D::WebServer::websocket_data_handler ( struct mg_connection *  conn,
int  flags,
char *  data,
size_t  data_len,
void *  user_data 
)
staticprotected

◆ websocket_ready_handler()

static void G3D::WebServer::websocket_ready_handler ( struct mg_connection *  conn,
void *  user_data 
)
staticprotected

Member Data Documentation

◆ DEFAULT_PORT

const uint16 G3D::WebServer::DEFAULT_PORT = 8080
static

◆ m_context

mg_context* G3D::WebServer::m_context
protected

◆ m_socketSchemeTable

Table<String, SocketScheme*> G3D::WebServer::m_socketSchemeTable
protected

Maps URI schemes to SocketSchemes.

Protected by m_socketTableMutex

◆ m_socketTable

Table<mg_connection*, shared_ptr<WebSocket> > G3D::WebServer::m_socketTable
protected

Protected by m_socketTableMutex.

◆ m_socketTableMutex

std::mutex G3D::WebServer::m_socketTableMutex
mutableprotected

◆ m_specification

Specification G3D::WebServer::m_specification
protected

documentation generated on Wed Nov 24 2021 08:02:02 using doxygen 1.8.15