仓库包含如下几个目录:
- docs: 文档
- examples: 示例程序演示如何为WebSocket客户端和服务器构建一些常用模式的基本版本。
- test: 单元测试确认您的代码正常工作,并帮助检测平台特定的问题。
- tutorials: 一组示例程序的详细演练。
- websocketpp: 所有库代码和默认配置文件。
WebSocket ++是仅包含头文件的库。 您可以通过在项目的包含路径中包含websocketpp源目录,并在程序中包含适当的WebSocket++头文件,来开始使用它。 您可能还需要包含和/或链接到适当的Boost/系统库。
在examples下有echo_server和echo_client,一般作为入门的例子。
下面在Windows上来运行这个例子。
首先,下载boost:https://www.boost.org/users/download/
接下来编译boost:
打开命令行,运行bootstrap.bat
接下来运行b2.exe
不出意外的话,就编好了(我这里都是默认配置来编译的,使用的是VS2019)
VS2019中新建websocketpp_server console
删掉main.cpp,将echo_server.cpp拷贝过来加入到工程中去
VS2019中新建websocketpp_client console
删掉main.cpp,将echo_client.cpp拷贝过来加入到工程中去
工程设置:
Include路径:
D:\GitWork\boost_1_72_0
D:\GitWork\websocketpp-master
Lib路径:
D:\GitWork\boost_1_72_0\stage\lib
链接libboost_date_time-vc142-mt-gd-x64-1_72.lib
Debug x64配置构建运行:
可以使用在线WebSocket网站进行测试
服务端收到信息:
如果要在Qt中使用,可参考以下pro配置:
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
DEFINES += QT_DEPRECATED_WARNINGS
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
echo_handler.hpp # 没用到,里面内容可注释掉
SOURCES += \
echo_server.cpp
win32: LIBS += -L$$PWD/../../../boost_1_72_0/stage/lib/ -llibboost_date_time-vc142-mt-gd-x64-1_72
win32: LIBS += -lWs2_32 \
-lwsock32
INCLUDEPATH += $$PWD/../../
INCLUDEPATH += $$PWD/../../../boost_1_72_0
DEPENDPATH += $$PWD/../../../boost_1_72_0/stage
关于主动发送信息,可以使用send方法,可以开一个线程,如果有内容,就发送,broadcast_server可以参考一下:
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <iostream>
#include <set>
/*#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;
/* on_open insert connection_hdl into channel
* on_close remove connection_hdl from channel
* on_message queue send to all channels
*/
enum action_type {
SUBSCRIBE,
UNSUBSCRIBE,
MESSAGE
};
struct action {
action(action_type t, connection_hdl h) : type(t), hdl(h) {}
action(action_type t, connection_hdl h, server::message_ptr m)
: type(t), hdl(h), msg(m) {}
action_type type;
websocketpp::connection_hdl hdl;
server::message_ptr msg;
};
class broadcast_server {
public:
broadcast_server() {
// Initialize Asio Transport
m_server.init_asio();
// Register handler callbacks
m_server.set_open_handler(bind(&broadcast_server::on_open,this,::_1));
m_server.set_close_handler(bind(&broadcast_server::on_close,this,::_1));
m_server.set_message_handler(bind(&broadcast_server::on_message,this,::_1,::_2));
}
void run(uint16_t port) {
// listen on specified port
m_server.listen(port);
// Start the server accept loop
m_server.start_accept();
// Start the ASIO io_service run loop
try {
m_server.run();
} catch (const std::exception & e) {
std::cout << e.what() << std::endl;
}
}
void on_open(connection_hdl hdl) {
{
lock_guard<mutex> guard(m_action_lock);
//std::cout << "on_open" << std::endl;
m_actions.push(action(SUBSCRIBE,hdl));
}
m_action_cond.notify_one();
}
void on_close(connection_hdl hdl) {
{
lock_guard<mutex> guard(m_action_lock);
//std::cout << "on_close" << std::endl;
m_actions.push(action(UNSUBSCRIBE,hdl));
}
m_action_cond.notify_one();
}
void on_message(connection_hdl hdl, server::message_ptr msg) {
// queue message up for sending by processing thread
{
lock_guard<mutex> guard(m_action_lock);
//std::cout << "on_message" << std::endl;
m_actions.push(action(MESSAGE,hdl,msg));
}
m_action_cond.notify_one();
}
void process_messages() {
while(1) {
unique_lock<mutex> lock(m_action_lock);
while(m_actions.empty()) {
m_action_cond.wait(lock);
}
action a = m_actions.front();
m_actions.pop();
lock.unlock();
if (a.type == SUBSCRIBE) {
lock_guard<mutex> guard(m_connection_lock);
m_connections.insert(a.hdl);
} else if (a.type == UNSUBSCRIBE) {
lock_guard<mutex> guard(m_connection_lock);
m_connections.erase(a.hdl);
} else if (a.type == MESSAGE) {
lock_guard<mutex> guard(m_connection_lock);
con_list::iterator it;
for (it = m_connections.begin(); it != m_connections.end(); ++it) {
m_server.send(*it,a.msg);
}
} else {
// undefined.
}
}
}
private:
typedef std::set<connection_hdl,std::owner_less<connection_hdl> > con_list;
server m_server;
con_list m_connections;
std::queue<action> m_actions;
mutex m_action_lock;
mutex m_connection_lock;
condition_variable m_action_cond;
};
int main() {
try {
broadcast_server server_instance;
// Start a thread to run the processing loop
thread t(bind(&broadcast_server::process_messages,&server_instance));
// Run the asio loop with the main thread
server_instance.run(9002);
t.join();
} catch (websocketpp::exception const & e) {
std::cout << e.what() << std::endl;
}
}