当前位置:   article > 正文

使用libwebsocket搭建websocket服务器实例_lws_ll_fwd_insert

lws_ll_fwd_insert

 

                                                       webcomm与前端的通讯-websocket服务器操作流程

 

  1. #include <libwebsockets.h>
  2. #include <pthread.h>
  3. #include <string.h>
  4. #define MAX_PAYLOAD_SIZE (4096)
  5. /** 开启确认才有重发的机制 */
  6. typedef struct comm_packet {
  7. void *content; /* is malloc'd */
  8. size_t content_len;
  9. uint8_t enable_ack; /* 是否需要响应 0:不需要响应 1:需要响应 */
  10. uint8_t retry_cnt; /* 重发的次数(ms) */
  11. } comm_packet_t;
  12. /* one of these created for each message */
  13. typedef struct msg {
  14. comm_packet_t *payload; /* is malloc'd */
  15. size_t len;
  16. } msg_t;
  17. /* one of these is created for each client connecting to us */
  18. struct per_session_data__cetwebsocket {
  19. struct per_session_data__cetwebsocket *pss_list;
  20. struct lws *wsi;
  21. uint32_t tail;
  22. struct lws_ring *ring; /* ringbuffer holding unsent messages */
  23. };
  24. /* one of these is created for each vhost our protocol is used with */
  25. struct per_vhost_data__cetwebsocket {
  26. struct lws_context *context;
  27. struct lws_vhost *vhost;
  28. const struct lws_protocols *protocol;
  29. struct per_session_data__cetwebsocket *pss_list; /* linked-list of live pss*/
  30. };
  31. static int
  32. callback_protocol_cetwebsocket_server(struct lws *wsi, enum lws_callback_reasons reason,
  33. void *user, void *in, size_t len);
  34. #define LWS_PLUGIN_PROTOCOL_CETWEBSOCKET \
  35. { \
  36. "cet-webserver", /* "cet-webserver" */ \
  37. callback_protocol_cetwebsocket_server, \
  38. sizeof(struct per_session_data__cetwebsocket), \
  39. 2048, \
  40. 0, NULL, 0 \
  41. }
  42. static struct lws_protocols sg_protocols[] = {
  43. { "http", lws_callback_http_dummy, 0, 0 },
  44. LWS_PLUGIN_PROTOCOL_CETWEBSOCKET,
  45. { NULL, NULL, 0, 0 } /* terminator */
  46. };
  47. static int sg_interrupted = 0;
  48. /* destroys the message when
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/241803
推荐阅读
相关标签
  

闽ICP备14008679号