webcomm与前端的通讯-websocket服务器操作流程
#include <libwebsockets.h>
#include <pthread.h>
#include <string.h>
#define MAX_PAYLOAD_SIZE (4096)
/** 开启确认才有重发的机制 */
typedef struct comm_packet {
void *content; /* is malloc'd */
size_t content_len;
uint8_t enable_ack; /* 是否需要响应 0:不需要响应 1:需要响应 */
uint8_t retry_cnt; /* 重发的次数(ms) */
} comm_packet_t;
/* one of these created for each message */
typedef struct msg {
comm_packet_t *payload; /* is malloc'd */
size_t len;
} msg_t;
/* one of these is created for each client connecting to us */
struct per_session_data__cetwebsocket {
struct per_session_data__cetwebsocket *pss_list;
struct lws *wsi;
uint32_t tail;
struct lws_ring *ring; /* ringbuffer holding unsent messages */
};
/* one of these is created for each vhost our protocol is used with */
struct per_vhost_data__cetwebsocket {
struct lws_context *context;
struct lws_vhost *vhost;
const struct lws_protocols *protocol;
struct per_session_data__cetwebsocket *pss_list; /* linked-list of live pss*/
};
static int
callback_protocol_cetwebsocket_server(struct lws *wsi, enum lws_callback_reasons reason,
void *user, void *in, size_t len);
#define LWS_PLUGIN_PROTOCOL_CETWEBSOCKET \
{ \
"cet-webserver", /* "cet-webserver" */ \
callback_protocol_cetwebsocket_server, \
sizeof(struct per_session_data__cetwebsocket), \
2048, \
0, NULL, 0 \
}
static struct lws_protocols sg_protocols[] = {
{ "http", lws_callback_http_dummy, 0, 0 },
LWS_PLUGIN_PROTOCOL_CETWEBSOCKET,
{ NULL, NULL, 0, 0 } /* terminator */
};
static int sg_interrupted = 0;
/* destroys the message when