| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- #pragma once
- #include <websocketpp/config/asio.hpp>
- #include <websocketpp/server.hpp>
- #include <websocketpp/common/thread.hpp>
- #include <websocketpp/common/memory.hpp>
- #include <boost/bind.hpp>
- #include <functional>
- #include <iostream>
- #include <string>
- #include <codecvt>
- #include <thread>
- #include <unordered_map>
- #include <mutex>
- #include <regex>
- #include <json/json.h>
- //#pragma comment(lib,"libssl.lib")
- //#pragma comment(lib,"libcrypto.lib")
- //#pragma comment(lib,"Crypt32.lib")
- typedef websocketpp::server<websocketpp::config::asio> server_plain;
- typedef websocketpp::server<websocketpp::config::asio_tls> server_tls;
- typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
- enum WEBSOCKET_TYPE
- {
- NORMAL = 0,
- TLS = 1,
- };
- class CWebSocketServer
- {
- public:
- CWebSocketServer();
- ~CWebSocketServer();
- bool init(uint16_t port ,uint16_t tlsPort ,std::string sslFile,std::string sslPwd);
- bool sendMsg(websocketpp::connection_hdl hdl, std::string msg);
- bool sendMsg(long conID, std::string msg);
- void run();
- void asyncRun();
- void close(websocketpp::connection_hdl hdl); // 关闭与指定客户端的连接
- void stop();
- typedef std::function<void(const long&, const std::string &, const std::string &)> Callback_RecvMsg;
- typedef std::function<void(const long&)> Callback_Close;
- void setCallbackRecvMsg(Callback_RecvMsg pRecvFun) { this->pRecvFun = pRecvFun; };
- void setCallbackClose(Callback_Close pCloseFun) { this->pCloseFun = pCloseFun; };
- private:
- template <typename EndpointType>
- void on_open(EndpointType* s, websocketpp::connection_hdl hdl, WEBSOCKET_TYPE type) {
- const long conID = reinterpret_cast<long>(hdl.lock().get());
- std::unique_lock<std::mutex>lock(m_mut);
- m_servers.insert(std::make_pair(hdl.lock().get(), type));
- m_conIds.insert(std::make_pair(conID,hdl));
- }
- template <typename EndpointType>
- void on_close(EndpointType* s, websocketpp::connection_hdl hdl, WEBSOCKET_TYPE type) {
- const long conID = reinterpret_cast<long>(hdl.lock().get());
- if (this->pCloseFun != nullptr)
- this->pCloseFun(conID); // 连接断开回调函数
- std::unique_lock<std::mutex>lock(m_mut);
- m_servers.erase(hdl.lock().get());
- m_conIds.erase(conID);
- }
- template <typename EndpointType>
- void on_message(EndpointType* s, websocketpp::connection_hdl hdl, typename EndpointType::message_ptr msg) {
- const long conID = reinterpret_cast<long>(hdl.lock().get());
- const std::string msgInfo = std::move(msg->get_payload());
- const std::string str = std::regex_replace(s->get_con_from_hdl(hdl)->get_remote_endpoint(), std::regex("::ffff:"),"");
- if (this->pRecvFun != nullptr && !msgInfo.empty())
- this->pRecvFun(conID, msgInfo, str); // 收到消息回调函数
- }
- std::string get_password() {
- return m_sslPwd;
- }
- context_ptr on_tls_init(websocketpp::connection_hdl hdl) {
- std::cout << "on_tls_init called with hdl: " << hdl.lock().get() << std::endl;
- context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23_server);
- try {
- ctx->set_options(boost::asio::ssl::context::default_workarounds |
- boost::asio::ssl::context::no_sslv2 |
- boost::asio::ssl::context::no_sslv3 |
- boost::asio::ssl::context::single_dh_use);
- ctx->set_password_callback(bind(&CWebSocketServer::get_password,this));
- ctx->use_certificate_chain_file(m_sslFile);
- ctx->use_private_key_file(m_sslFile, boost::asio::ssl::context::pem);
- }
- catch (std::exception& e) {
- char str[1024];
- snprintf(str, 1024, "[%s :%d][ %s]", __FUNCTION__, __LINE__, e.what());
- std::cout << str << std::endl;
- }
- return ctx;
- }
- private:
- boost::asio::io_service ios;
- websocketpp::lib::shared_ptr<websocketpp::lib::thread> m_Thread; // 线程
- server_plain endpoint_plain;
- server_tls endpoint_tls;
- std::string m_sslFile;
- std::string m_sslPwd;
- std::unordered_map<void*, WEBSOCKET_TYPE> m_servers;
- std::unordered_map<long, websocketpp::connection_hdl> m_conIds;
- std::mutex m_mut; //map锁
- Callback_RecvMsg pRecvFun; // 接收消息回调函数
- Callback_Close pCloseFun; // 连接断开回调函数
- };
|