使用websocketpp,需要安装boost。
解压 boost_1_73_0.tar.gz
tar -zvfx boost_1_73_0.tar.gz
运行bootstrap.sh后,生成b2,直接运行b2;如果需要移植到ARM平台,则需要在project-config.jam文件将using gcc修改如下:
using gcc : : arm-linux-gnueabihf-gcc;
生成的库文件在stage目录中。
下面是websocket服务器例程,该服务器功能用于推送数据到客户端,对连接到服务器的客户端,服务器定时推送数据到客户端。这里简单实现了两个推送地址,分别为ws://localhost:9000/push/helloworld和ws://localhost:9000/current/status。
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <set>
#include <string>
#include <iostream>
/*
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>*/
#include <websocketpp/common/thread.hpp>
typedef websocketpp::server<websocketpp::config::asio> server;
using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
using websocketpp::lib::thread;
using websocketpp::lib::mutex;
using websocketpp::lib::lock_guard;
using websocketpp::lib::unique_lock;
using websocketpp::lib::condition_variable;
enum action_type {
SUBSCRIBE,
UNSUBSCRIBE,
MESSAGE
};
typedef server::message_ptr message_ptr;
struct action {
action(action_type t, std::string s, connection_hdl h) : type(t), struri(s), hdl(h) {}
action(action_type t, std::string s, connection_hdl h, server::message_ptr m)
: type(t), struri(s), hdl(h), msg(m) {}
action_type type;
std::string struri;
websocket

本文详细介绍使用websocketpp库在本地搭建WebSocket服务器的过程,并实现定时向客户端推送数据的功能。通过具体代码示例,展示了如何设置连接回调、消息处理及定时任务,适用于希望深入理解WebSocket服务器开发的读者。
最低0.47元/天 解锁文章
8432

被折叠的 条评论
为什么被折叠?



