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)
相关推荐
ONLYOFFICE4 分钟前
ONLYOFFICE 10.0:深入了解最新的 40 项安全修复
网络·安全
上海云盾-高防顾问10 分钟前
边缘验签是什么
运维·服务器·网络
CH_Qing38 分钟前
lspci 常见debug 命令
网络
码林鼠1 小时前
flutter网络http请求入门教程-dio
网络协议·flutter·http
天南散修1 小时前
《802.11-2024》第08讲 · 聚合与块确认:把「一次一帧」变成「一次一批」
linux·网络·wifi·802.11
企业解惑小能手-域智盾系统1 小时前
终端安全管理软件怎么选?终端安全管理不是“装个客户端”:从底层逻辑看企业怎么选
服务器·网络·安全·域智盾软件
济南凡维2 小时前
智能相机 VS 工业相机:机器视觉选型一次讲透
网络·数码相机·视觉检测
kxx6662 小时前
网络原理(初识)
网络
天南散修2 小时前
《802.11-2024》第06讲 · 介质接入:MAC 架构与 DCF(CSMA/CA)
linux·网络·wifi·802.11
QYR-分析3 小时前
2.80 亿增至 4.12 亿美元,CAGR5.6%:肌电诱发电位仪稳健扩容,神经电生理检测设备加速走向便携与数字化
大数据·网络·人工智能