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)
相关推荐
汤愈韬10 分钟前
ACL概述、ACL原理、基本ACL应用及配置
网络·网络协议·网络安全
码刘的极客手记4 小时前
VCAP4-DCA Beta 考试体验分享与 esxcli 自动化实战(第二、三部分)
网络·esxi·vmware·虚拟机
朝风工作室5 小时前
开源一款轻量级跨网段 IPC 设备发现与网络配置工具
网络·开源
Qaz555666915 小时前
透明DNS选路实验
网络
haluhalu.6 小时前
Linux网络编程------网络基础
网络·网络协议
坐怀不乱杯魂6 小时前
Linux网络 - HTTP协议
网络·网络协议·http
ipooipoo11888 小时前
动态住宅IP的定义、核心优势与技术场景应用
网络·网络协议·tcp/ip
祁鱼鱼鱼鱼鱼8 小时前
Keepalived实验环境设定
linux·服务器·网络
滴水之功8 小时前
OpenWrt的WAN和LAN的切换
linux·网络·openwrt
莹莹学编程—成长记10 小时前
预备知识socket
网络