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)
相关推荐
snow@li几秒前
Telnet 全景教程:全平台安装、命令使用、故障排查与安全规范
运维·网络
随风而飘18639 分钟前
罗德与施瓦茨 FSP13 中高端频谱分析仪
网络·功能测试·测试工具
SendTomo1 小时前
SendTomo稳定传输三大核心策略
javascript·网络协议·webrtc·html5·p2p
旗开得胜马到成功1 小时前
AMD TPM 8.3/8.5漏洞技术剖析:服务器加密信任根崩塌后的备份密钥泄露风险与紧急加固
运维·服务器·网络·系统架构
CANWeb冗余现场总线1 小时前
2毫秒扫描周期以太网Modbus TCP UDP高速通信:西门子SMART PLC+CANWeb现场总线远程扩展IO模块
网络协议·tcp/ip·udp
阿pin1 小时前
HTTP 与 HTTPS 详解
网络协议·http·https
阿pin2 小时前
HTTP 协议的演进
网络·网络协议·http
bksczm2 小时前
Linux之网络层协议(IP协议)
linux·网络·tcp/ip
跨境技工小黎2 小时前
IPFoxy动态住宅IP实测:做数据采集和爬虫可行吗?
爬虫·网络协议·tcp/ip
Mortalbreeze2 小时前
深入理解TCP协议(一):TCP报文格式详解
linux·服务器·网络·tcp/ip