增加websocket支持

This commit is contained in:
xx
2024-06-19 03:01:08 +08:00
parent d5818c3299
commit ec4df79c9e
8 changed files with 336 additions and 3 deletions

View File

@@ -9,7 +9,7 @@
#define DEBUG_MEM 0
#define USE_OPENSSL 0
#define USE_OPENSSL 1
using namespace ylib;

View File

@@ -35,11 +35,13 @@
#define USE_NET_HTTP_CLIENT 1
#define USE_NET_HTTP_UTIL 1
#define USE_NET_HTTP_AGENT 0
#define USE_NET_HTTP_WS 1
#else
#define USE_NET_HTTP_WEBSITE 0
#define USE_NET_HTTP_CLIENT 0
#define USE_NET_HTTP_UTIL 0
#define USE_NET_HTTP_AGENT 0
#define USE_NET_HTTP_WS 0
#endif

View File

@@ -0,0 +1,70 @@
#pragma once
#include "http_define.h"
#if USE_NET_HTTP_WS == 1
#include <functional>
#include "base/define.h"
#include "base/error.h"
class wsserver_lst;
namespace ylib
{
namespace network
{
namespace http
{
/// <summary>
/// WebSocket服务器
/// </summary>
class wsserver:public ylib::error_base {
public:
wsserver();
~wsserver();
/// <summary>
/// 启动
/// </summary>
/// <param name="port"></param>
/// <param name="ssl"></param>
/// <param name="ssl_key"></param>
/// <param name="ssl_pem"></param>
/// <returns></returns>
bool start(ushort port, bool ssl = false,const std::string& ssl_key = "", const std::string& ssl_pem = "");
/// <summary>
/// 停止
/// </summary>
void stop();
/// <summary>
/// 发送
/// </summary>
bool send(int64 conn, std::string_view value);
void on_accept(std::function<bool(int64)> callback) { m_callback_accept = callback; }
void on_recv(std::function<bool(int64,const char* data, int len)> callback) { m_callback_recv = callback; }
void on_close(std::function<void(int64)> callback) { m_callback_close = callback; }
friend class wsserver_lst;
private:
// 端口
ushort m_port = 0;
// 是否为SSL
bool m_ssl = false;
// SSL-KEY
std::string m_ssl_key;
// SSL-PEM
std::string m_ssl_pem;
// 监听器
wsserver_lst* m_listener = nullptr;
// HPServer
void* m_server = nullptr;
std::function<bool(int64)> m_callback_accept;
std::function<bool(int64,const char* data, int len)> m_callback_recv;
std::function<void(int64)> m_callback_close;
};
}
}
}
#endif

View File

@@ -6,6 +6,7 @@ namespace ylib
{
namespace codec
{
ylib::buffer sha1(const ylib::buffer& data);
std::string md5(const ylib::buffer& data);
std::string to_utf8(const std::string& gbk);
std::string to_gbk(const std::string& utf8);