Support Forum G3D Web Page |
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< WebServer > | create (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< WebSocket > | socketFromConnection (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 |
Web server with support for https and websockets
typedef std::function<shared_ptr<WebSocket>WebServer*, mg_connection*, const NetAddress&)> G3D::WebServer::SocketFactory |
|
protected |
|
protectedvirtual |
|
static |
The server is not started on creation.
Invoke start().
|
inlinestaticprotectedinherited |
Like std::make_shared, but works for protected constructors.
Call as createShared<myclass>.
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.
uri | The uri parameter passed to registerWebSocketHandler to facilitate the creation of these sockets. |
|
inlineprotectedvirtual |
|
inlineprotectedvirtual |
|
inlineprotectedvirtual |
|
inlineprotectedvirtual |
Default implementation invokes socket->onReady.
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);
|
protected |
void G3D::WebServer::start | ( | ) |
void G3D::WebServer::stop | ( | ) |
The destructor automatically invokes stop().
|
staticprotected |
|
staticprotected |
|
staticprotected |
|
staticprotected |
|
static |
|
protected |
|
protected |
Maps URI schemes to SocketSchemes.
Protected by m_socketTableMutex
Protected by m_socketTableMutex.
|
mutableprotected |
|
protected |