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)
相关推荐
Jony_2 天前
高可用移动网络连接
网络协议
chilix3 天前
Linux 跨网段路由转发配置
网络协议
DianSan_ERP4 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
呉師傅4 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑
gihigo19984 天前
基于TCP协议实现视频采集与通信
网络协议·tcp/ip·音视频
2501_946205524 天前
晶圆机器人双臂怎么选型?适配2-12寸晶圆的末端效应器有哪些?
服务器·网络·机器人
linux kernel4 天前
第七部分:高级IO
服务器·网络
数字护盾(和中)4 天前
BAS+ATT&CK:企业主动防御的黄金组合
服务器·网络·数据库
~远在太平洋~4 天前
Debian系统如何删除多余的kernel
linux·网络·debian
unfeeling_4 天前
Keepalived实验
linux·服务器·网络