This package implements the Websocket protocol as described in the RFC 6455. It allow to define callbacks on specifics events and also implements a broadcaster service to make easier distribute message among different active connections.
Initialization
In order to make use of websocket package, two simple steps are required: the first one is to include the package header file on top of your code, then perform an explicit package inside duda_main() function.
Example:
/* Include package header */ #include "packages/websocket/websocket.h" int duda_main() { ... duda_load_package(websocket, "websocket"); ... /* * In case you want to use the websocket broadcast feature you must enable the * broadcaster service: */ websocket->broadcaster(); ... return 0; }
Once the package is loaded, you can use any methods from your callbacks in the following way:
websocket->method(...)
It sends a message in broadcast mode to all active connections registered under a specific channel. This method is intended to be used inside a websocket callback. If you look for a method to broadcast all connection from a worker please review the method broadcast_all().
Prototype
int broadcast(ws_request_t *wr, unsigned char *data, uint64_t len, int msg_type, int channel);Parameters
It sends a message in broadcast mode to all active connections registered under a specific channel number. This method is intended to be used from a worker.
Prototype
int broadcast_all(unsigned char *data, uint64_t len, int msg_type, int channel);Parameters
Initialize the websocket broadcaster service and interfaces. This method must be invoked inside duda_main().
Prototype
int broadcaster();Parameters None Return: On success it returns 0, on error this method perform an explicit exit.
It perform the websocket handshake and connection upgrade. This must be used inside a normal HTTP callback function. It will take care of the handshake details and response data.
Prototype
int handshake(duda_request_t *dr, int channel);Parameters
It set a specific callback function against on a received event.
Prototype
int set_callback(int type, void (*callback) (duda_request_t *, struct ws_request *));Parameters
It writes a message frame to a specified websocket connection.
Prototype
int write(struct ws_request *wr, unsigned int code, unsigned char *data, uint64_t len);Parameters