forked from tashaxing/CppHttpDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_server.h
More file actions
45 lines (37 loc) · 1.65 KB
/
Copy pathhttp_server.h
File metadata and controls
45 lines (37 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#pragma once
#include <string>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include "../common/mongoose.h"
// 定义http返回callback
typedef void OnRspCallback(mg_connection *c, std::string);
// 定义http请求handler
using ReqHandler = std::function<bool (std::string, std::string, mg_connection *c, OnRspCallback)>;
class HttpServer
{
public:
HttpServer() {}
~HttpServer() {}
void Init(const std::string &port); // 初始化设置
bool Start(); // 启动httpserver
bool Close(); // 关闭
void AddHandler(const std::string &url, ReqHandler req_handler); // 注册事件处理函数
void RemoveHandler(const std::string &url); // 移除时间处理函数
static std::string s_web_dir; // 网页根目录
static mg_serve_http_opts s_server_option; // web服务器选项
static std::unordered_map<std::string, ReqHandler> s_handler_map; // 回调函数映射表
private:
// 静态事件响应函数
static void OnHttpWebsocketEvent(mg_connection *connection, int event_type, void *event_data);
static void HandleHttpEvent(mg_connection *connection, http_message *http_req);
static void SendHttpRsp(mg_connection *connection, std::string rsp);
static int isWebsocket(const mg_connection *connection); // 判断是否是websoket类型连接
static void HandleWebsocketMessage(mg_connection *connection, int event_type, websocket_message *ws_msg);
static void SendWebsocketMsg(mg_connection *connection, std::string msg); // 发送消息到指定连接
static void BroadcastWebsocketMsg(std::string msg); // 给所有连接广播消息
static std::unordered_set<mg_connection *> s_websocket_session_set; // 缓存websocket连接
std::string m_port; // 端口
mg_mgr m_mgr; // 连接管理器
};