使用 mongoose 搭建Websocket 服务端示例代码

使用 mongoose 搭建Websocket 服务端示例代码

 1 struct mg_per_session_data
 2 {
 3     time_t tmLastSendHeart;
 4     time_t tmLastRecvHeart;
 5 }
 6 
 7 static void fn(struct mg_connection* c, int ev, void* ev_data, void* fn_data) {
 8 
 9     time_t tmNow;
10     time(&tmNow);
11     if (ev == MG_EV_POLL)
12     {
13         //time_t tNow;
14         //time(&tNow);
15         //TRACE("%d\r\n", tNow);
16         //if (c->next)
17         //    mg_ws_send(c->next, nullptr, 0, WEBSOCKET_OP_PING);
18         if (c->fn_data)
19         {
20             auto pMsg = (mg_per_session_data*)c->fn_data;
21 
22             if (tmNow - pMsg->tmLastSendHeart > 30)
23             {
24                 pMsg->tmLastSendHeart = tmNow;
25                 mg_ws_send(c, nullptr, 0, WEBSOCKET_OP_PING);
26             }
27         }
28     }
29     /*if (ev == MG_EV_OPEN) {
30          c->is_hexdumping = 1;
31     }*/
32     else if (ev == MG_EV_HTTP_MSG) {
33         struct mg_http_message* hm = (struct mg_http_message*)ev_data;
34         mg_ws_upgrade(c, hm, NULL);   // Upgrade HTTP to WS
35     }
36     else if (ev == MG_EV_WS_MSG) {
37         // Got websocket frame. Received data is wm->data. Echo it back!
38         struct mg_ws_message* wm = (struct mg_ws_message*)ev_data;
39         //mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_TEXT);
40         char* json = nullptr;
41         auto doc = yyjson_read(wm->data.ptr, wm->data.len, 0);   //使用yyjson读取参数
42         //TODO
43         yyjson_doc_free(doc);
44     }
45     else if (ev == MG_EV_WS_OPEN)
46     {
47         //websocket 链接时 为每个链接创建用户数据
48         if (c->fn_data == nullptr)
49             c->fn_data = new mg_per_session_data;
50     }
51     else if (ev == MG_EV_WS_CTL)
52     {
53         struct mg_ws_message* wm = (struct mg_ws_message*)ev_data;
54         auto op = wm->flags & 15;
55         if (op == WEBSOCKET_OP_CLOSE)
56         {
57             //关闭消息 释放内存数据
58             if (c->fn_data)
59             {
60                 delete (mg_per_session_data*)(c->fn_data);
61                 c->fn_data = nullptr;
62             }
63         }
64         else if (op == WEBSOCKET_OP_PONG)
65         {
66             if (c->fn_data) ((mg_per_session_data*)c->fn_data)->tmLastRecvHeart = tmNow;
67         }
68     }
69     (void)fn_data;
70 }
71 
72 void ThreadProcForWebsocket(DWORD_PTR wparam, uint16_t lparam)
73 {
74     //CMGWSServer* pThis = (CMGWSServer*)wparam;
75     //if (!pThis->m_bThreadWork) return;
76     struct mg_mgr m_mg_mgr = { 0 }; //Event management structure
77     mg_mgr_init(&pThis->m_mg_mgr);
78     char url[128];
79     sprintf_s(url, 128, "ws://0.0.0.0:%d", lparam);
80     auto pConn = mg_http_listen(&pThis->m_mg_mgr, url, fn, nullptr);//用户自定义数据赋值为空
81     if (pConn)    do { mg_mgr_poll(&pThis->m_mg_mgr, 100); } while (/*pThis->m_bThreadWork*/true);
82     mg_mgr_free(&pThis->m_mg_mgr);
83     //pThis->m_bThreadWork = FALSE;
84 }

 

posted on 2023-03-14 10:47  Sam.Richard  阅读(521)  评论(0编辑  收藏  举报

导航