下面是websocket客户端例程,该客户端功能用于接收websocket服务器推送的数据。这里简单实现了两个连接服务器的请求,请求地址分别为ws://localhost:9000/push/helloworld和ws://localhost:9000/current/status。
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <websocketpp/extensions/permessage_deflate/enabled.hpp>
#include <iostream>
#define helloworld_uri "/push/helloworld"
#define current_status_uri "/current/status"
struct deflate_config : public websocketpp::config::asio_client {
typedef deflate_config type;
typedef asio_client base;
typedef base::concurrency_type concurrency_type;
typedef base::request_type request_type;
typedef base::response_type response_type;
typedef base::message_type message_type;
typedef base::con_msg_manager_type con_msg_manager_type;
typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;
typedef base::alog_type alog_type;
typedef base::elog_type elog_type;
typedef base::rng_type rng_type;
struct transport_config : public base::transport_config {
typedef type::concurrency_type concurrency_type;
typedef type::alog_type alog_type;
typedef type::elog_type elog_type;
typedef type::request_type request_type;
typedef type::response_type response_type;
typedef websocketpp::transport::asio::basic_socket::endpoint
socket_type;
};
typedef websocketpp::transport::asio::endpoint<transport_config>
transport_type;
/// permessage_compress extension
struct permessage_deflate_config {};
typedef websocketpp::extensions::permessage_deflate::enabled
<permessage_deflate_config> permessage_deflate_type;
};
typedef websocketpp::client<deflate_config> client;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
// pull out the type of messages sent by our config
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
void hello_world(websocketpp::connection_hdl, client::message_ptr msg) {
std::cout << msg->get_payload() << std::endl;
}
void current_status(websocketpp::connection_hdl, client::message_ptr msg) {
std::cout << msg->get_payload() << std::endl;
}
void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) {
client::connection_ptr con = c->get_con_from_hdl(hdl);
if (con->get_resource() == helloworld_uri) {
system_health_status_message(hdl, msg);
} else if (con->get_resource() == "/current/status") {
device_status_message(hdl, msg);
} else {
std::cout << "Detected:" << msg->get_payload() << std::endl;
}
}
int main(int argc, char* argv[]) {
client m_client;
std::string uri = "ws://localhost:9000";
try {
//设置日志功能
m_client.clear_access_channels(websocketpp::log::alevel::all);
m_client.clear_error_channels(websocketpp::log::elevel::all);
// 初始化异步IO
m_client.init_asio();
// 注册我们的消息处理程序
m_client.set_message_handler(bind(&on_message, &m_client, ::_1, ::_2));
websocketpp::lib::error_code ec;
client::connection_ptr con = m_client.get_connection(uri+helloworld_uri, ec);
if (ec) {
std::cout << "could not create connection because: " << ec.message() << std::endl;
return -1;
}
m_client.connect(con);
/**************************************************************************/
client::connection_ptr con1 = m_client.get_connection(uri+current_status_uri, ec);
if (ec) {
std::cout << "could not create connection because: " << ec.message() << std::endl;
return -1;
}
m_client.reset();
m_client.connect(con1);
m_client.run();
} catch (websocketpp::exception const & e) {
std::cout << e.what() << std::endl;
}
return 0;
}
编译命令:
g++ -std=gnu++14 -o client websocket_client.cpp -I/DIR/websocketpp -I/DIR/boost/include -L/DIR/boost/lib -lboost_system -lboost_chrono -lpthread -lz