2021-6-6 今天改成了c++ 20 協程
具體修改:改成c++ 20 協程 删除boost庫
c++ 2019協程
在vs2019 裡面 加上/await
gcc10 版本
gcc10 版本以上 需要fcoroution 支援
代碼已經上傳
增加websocket html 測試 [2021-6-6]
在代碼目錄下 testwebsocket.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>websocket 測試</title>
<script type="text/javascript">
function WebSocketTest()
{
if ("WebSocket" in window)
{
console.log("!");
// 打開一個 web socket
var ws = new WebSocket("ws://127.0.0.1:6001/echo");
ws.onopen = function()
{
// Web Socket 已連接配接上,使用 send() 方法發送資料
ws.send("data here");
};
ws.onmessage = function (evt)
{
var received_msg = evt.data;
console.log(received_msg);
};
ws.onclose = function()
{
// 關閉 websocket
alert("連接配接已關閉...");
};
}
else
{
console.log("不支援 WebSocket!");
}
}
</script>
</head>
<body>
<div id="sse">
<a href="javascript:WebSocketTest()">運作 WebSocket</a>
</div>
</body>
</html>
目的
從web上傳圖檔和視訊,httpserver背景接收,進行圖檔識别并傳回
使用架構
1 asio
2 websocketpp
3 websock 【紫丁香研發】
4 opencv
5 調用python
httpserver和websocket server
第一版使用的httpserver 和 websocket server 是由websocketpp制作的
路由
比如nodejs 的 express 或 koi
比如go 語言的iris 等等那種路由方式很理想
這裡模仿這種方式,使用c++11 的lamba
void registerhttp()
{
broadcast_server& _httpserver = _hserver;
_httpserver.func_proc_register("/login", [](maps_pair& rp) {
auto iter = rp.begin();
while (iter != rp.end())
{
std::cout << iter->first << " " << iter->second << std::endl;
}
return;
});
_httpserver.func_proc_register("/url", [](maps_pair& rp) {
cout << "url get" << endl;
return;
});
}
int main(int argc, char** argv)
{
registerhttp();
_hserver.Start(80);
_hserver.Join();
return 0;
}
實作
bool func_proc_register(std::string uri, handler_2http f) {
auto it = v_map_function.find(uri);
if (v_map_function.end() != it)
return false;
route_request rr;
rr.func = f;
return v_map_function.emplace(uri, rr).second;
}
實際上,在類内部插入了一個回調函數vmap,在使用者浏覽上輸入後,背景從url位址中擷取所有參數,并進行比對。
void on_http(websocketpp::connection_hdl hdl)
{
try
{
c_server::connection_ptr con = m_server.get_con_from_hdl(hdl);
websocketpp::http::parser::request rt = con->get_request();
const string& strUri = rt.get_uri();
const string& strMethod = rt.get_method();
const string& strBody = rt.get_body(); //隻針對post時有資料
const string& strHost = rt.get_header("host");
const string& strVersion = rt.get_version();
//std::cout << "接收到一個" << strMethod.c_str() << "請求:" << strUri.c_str() << " 線程ID=" << ::GetCurrentThreadId() << " host = " << strHost << std::endl;
if (strMethod.compare("POST") == 0)
{//對于post送出的,直接傳回跳轉
con->set_status(websocketpp::http::status_code::value(websocketpp::http::status_code::found));
con->append_header("location", "http://localhost");
}
else if (strMethod.compare("GET") == 0)
{
if (strUri.compare("/") == 0)
{//請求首頁
con->set_body("Hello We Meeting Here! Please use more route");
con->set_status(websocketpp::http::status_code::value(websocketpp::http::status_code::ok));
}
else if (strUri.compare("/favicon.ico") == 0)
{//請求網站圖示,讀取圖示檔案,直接傳回二進制資料
con->set_body("");
//con->append_header("Content-Type", "image/x-icon");
con->set_status(websocketpp::http::status_code::value(websocketpp::http::status_code::ok));
}
else if (strUri.compare("/test1") == 0)
{
con->set_body("test1 come here");
con->set_status(websocketpp::http::status_code::value(
websocketpp::http::status_code::ok));
}
else
{//其他未定義的頁面,傳回跳轉
cout <<"uri:"<<strUri << endl;
con->append_header("Access-Control-Allow-Origin", "*");
con->append_header("Access-Control-Allow-Methods", "GET,POST");
con->append_header("Access-Control-Allow-Headers", "x-requested-with,content-type");
std::map<string, string> list; //操作參數清單
string url;//路由參數
GetReqeustAndParmeter(strUri, url, list);
cout << "url is:" << url << endl;
auto iter = v_map_function.find(url);
if (iter == v_map_function.end())
{
string reply = "{\"ret\":-1,\"info\":\"no this parameter\"}";
con->set_body(reply.c_str());
con->set_status(websocketpp::http::status_code::value(websocketpp::http::status_code::ok));
return;
}
else {
route_request& rr = iter->second;
if (rr.func != nullptr)
{
rr.func(list);
}
con->set_body(rr.retString.c_str());
//con->set_body("{\"ret\":0}");
con->set_status(websocketpp::http::status_code::value(websocketpp::http::status_code::ok));
}
}
}
}
catch (websocketpp::exception const& e)
{
std::cout << "exception: " << e.what() << std::endl;
}
catch (std::exception& e)
{
std::cout << "exception: " << e.what() << std::endl;
}
catch (...)
{
}
}
适用系統
1 windows
vs2019
2 linux
編譯cmake
全部代碼:
紫丁香位址
在gitee 上。每個星期更新