simple websocket用法

项目地址在:https://gitlab.com/eidheim/Simple-Web-Server

我调试通过的代码如下:

cpp 复制代码
#include "server_ws.hpp"

using namespace std;

using WsServer = SimpleWeb::SocketServer<SimpleWeb::WS>;

void run_simple_socket_server(unsigned short port)
{
    WsServer server;
    server.config.port = port;

    auto &echo = server.endpoint["^/echo/?$"];
    echo.on_message = [](shared_ptr<WsServer::Connection> connection, shared_ptr<WsServer::Message> message)
    {
        auto message_str = message->string();

        cout << "Server: Message received: \"" << message_str << "\" from " << connection.get() << endl;

        cout << "Server: Sending message \"" << message_str << "\" to " << connection.get() << endl;

        auto send_stream = make_shared<WsServer::SendStream>();
        *send_stream << message_str;
        connection->send(send_stream, [](const SimpleWeb::error_code &ec)
                         {
                    if (ec) {
                        cout << "Server: Error sending message. " <<
                            "Error: " << ec << ", error message: " << ec.message() << endl;
                    } });
    };

    echo.on_open = [](shared_ptr<WsServer::Connection> connection)
    {
        cout << "Server: Opened connection " << connection.get() << endl;
    };

    echo.on_close = [](shared_ptr<WsServer::Connection> connection, int status, const string & /*reason*/)
    {
        cout << "Server: Closed connection " << connection.get() << " with status code " << status << endl;
    };

    echo.on_error = [](shared_ptr<WsServer::Connection> connection, const SimpleWeb::error_code &ec)
    {
        cout << "Server: Error in connection " << connection.get() << ". "
             << "Error: " << ec << ", error message: " << ec.message() << endl;
    };

    thread server_thread([&server]()
                         { server.start(); });
    server_thread.join();
}
int main(int argc, char const *argv[])
{
    unsigned short port = 10088;
    cout << "Server port: " << port << endl;
    run_simple_socket_server(port);
    return 0;
}

Makefile如下:

bash 复制代码
.PHONY : clean

ROOTDIR = ../..
INCLUDE = -I../$(ROOTDIR)/lib/inc -I$(ROOTDIR)/public -I$(ROOTDIR)/public/simple_websocket_server
LDLIBS = -lpthread -lssl -lcrypto -lboost_system
runlibs = -Wl,-rpath=/web/lib

dst = arm
ifeq ($(dst),arm)
CXX = aarch64-linux-gnu-g++ --std=c++11
LDLIBS += -L../$(ROOTDIR)/lib/so.aarch64
else
CXX = g++ --std=c++11
LDLIBS += -L../$(ROOTDIR)/lib/so.x86_64
endif

bin = ../../bin/simple_websocket

src = $(wildcard *.cpp)
obj = $(patsubst %.cpp,%.o, $(src))

$(bin) : $(obj)
	$(CXX) $(runlibs) $^ -o $@ $(LDLIBS)

$(obj): %.o : %.cpp
	$(CXX) -g -c $(INCLUDE) $< -o $@

clean :
	rm -f $(obj) $(bin)
相关推荐
枷锁—sha2 小时前
【SRC】SQL注入WAF 绕过应对策略(二)
网络·数据库·python·sql·安全·网络安全
Zach_yuan3 小时前
深入浅出 JSONCpp
linux·服务器·网络·c++
杨了个杨89823 小时前
memcached部署
qt·websocket·memcached
迎仔4 小时前
B-算力中心网络隔离的必要性:为什么必须隔离?
网络
野指针YZZ5 小时前
一键配置RK3588网络与SSH远程连接
网络·ssh·rk3588
迎仔5 小时前
10-网络安全监控与事件响应:数字世界的智能监控与应急系统
网络·安全·web安全
上海合宙LuatOS6 小时前
LuatOS核心库API——【audio 】
java·网络·单片机·嵌入式硬件·物联网·音视频·硬件工程
深圳市恒星物联科技有限公司7 小时前
水质流量监测仪:复合指标监测的管网智能感知设备
大数据·网络·人工智能
三水不滴8 小时前
有 HTTP 了为什么还要有 RPC?
经验分享·笔记·网络协议·计算机网络·http·rpc
科技块儿8 小时前
2026年我会推荐哪些IP归属地查询网站?
网络·ip地址·ip归属地·运维工具·网络工具·实用网站·2026工具推荐