websocketpp c++客户端

利用websocketpp库编写websocket客户端程序。

一、阻塞方式实例

#include "stdafx.h"

#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>

#include <iostream>

typedef websocketpp::client<websocketpp::config::asio_client> 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;

bool bIsConnectedServer = false;

// This message handler will be invoked once for each incoming message. It
// prints the message and then sends a copy of the message back to the server.
void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) {
	std::cout << "on_message called with hdl: " << hdl.lock().get()
		<< " and message: " << msg->get_payload()
		<< std::endl;


	websocketpp::lib::error_code ec;

	c->send(hdl, msg->get_payload(), msg->get_opcode(), ec);
	if (ec) {
		std::cout << "Echo failed because: " << ec.message() << std::endl;
	}
}

void on_open(client* c, websocketpp::connection_hdl hdl) {
	std::cout << "open handler" << std::endl;
	client::connection_ptr con = c->get_con_from_hdl(hdl);
	//websocketpp::config::core_client::request_type requestClient = con->get_request();
	if (con->get_ec().value() != 0)
	{
		bIsConnectedServer = false;
	}
	else
	{
		bIsConnectedServer = true;
	}
}

int main(int argc, char* argv[]) {
	// Create a client endpoint
	client c;

	//std::string uri = "ws://localhost:9100/websocket?userid=admin";
	std::string uri = "ws://localhost:9100";

	if (argc == 2) {
		uri = argv[1];
	}

	try {
		// Set logging to be pretty verbose (everything except message payloads)
		c.set_access_channels(websocketpp::log::alevel::all);
		c.clear_access_channels(websocketpp::log::alevel::frame_payload);

		// Initialize ASIO
		c.init_asio();
		c.set_reuse_addr(true);

		// Register our message handler
		c.set_message_handler(bind(&on_message, &c, ::_1, ::_2));
		c.set_open_handler(bind(&on_open, &c, ::_1));

		websocketpp::lib::error_code ec;
		client::connection_ptr con = c.get_connection(uri, ec);
		if (ec) {
			std::cout << "could not create connection because: " << ec.message() << std::endl;
			return 0;
		}

		// Note that connect here only requests a connection. No network messages are
		// exchanged until the event loop starts running in the next line.
		c.connect(con);

		// Start the ASIO io_service run loop
		// this will cause a single connection to be made to the server. c.run()
		// will exit when this connection is closed.
		c.run();
                c.stop();
	}
	catch (websocketpp::exception const & e) {
		std::cout << e.what() << std::endl;
	}
}

二、线程方式实例

#include "stdafx.h"

#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>

#include <iostream>

typedef websocketpp::client<websocketpp::config::asio_client> 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;

bool bIsConnectedServer = false;

// This message handler will be invoked once for each incoming message. It
// prints the message and then sends a copy of the message back to the server.
void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) {
	std::cout << "on_message called with hdl: " << hdl.lock().get()
		<< " and message: " << msg->get_payload()
		<< std::endl;


	websocketpp::lib::error_code ec;

	c->send(hdl, msg->get_payload(), msg->get_opcode(), ec);
	if (ec) {
		std::cout << "Echo failed because: " << ec.message() << std::endl;
	}
}
//连接上服务器,回调此函数
void on_open(client* c, websocketpp::connection_hdl hdl) {
	std::cout << "open handler" << std::endl;
	client::connection_ptr con = c->get_con_from_hdl(hdl);
	//websocketpp::config::core_client::request_type requestClient = con->get_request();
	if (con->get_ec().value() != 0)
	{
		bIsConnectedServer = false;
	}
	else
	{
		bIsConnectedServer = true;
	}
}

//服务器远程断开连接,回调此函数
void on_close(client* c, websocketpp::connection_hdl hdl)
{
	bIsConnectedServer = false;
        closeConnect();
}

//无法连接上服务器时,回调此函数,socket在内部已关闭上层无需再关闭
void on_termination_handler(client* c, websocketpp::connection_hdl hdl)
{
	bIsConnectedServer = false;
        closeConnect();
}

int main(int argc, char* argv[]) {
	// Create a client endpoint
	client c;

	//std::string uri = "ws://localhost:9100/websocket?userid=admin";
	std::string uri = "ws://localhost:9100";

	if (argc == 2) {
		uri = argv[1];
	}

	try {
		// Set logging to be pretty verbose (everything except message payloads)
		c.set_access_channels(websocketpp::log::alevel::all);
		c.clear_access_channels(websocketpp::log::alevel::frame_payload);

		// Initialize ASIO
		c.init_asio();
		c.set_reuse_addr(true);

		// Register our message handler
		c.set_message_handler(bind(&on_message, &c, ::_1, ::_2));
		c.set_open_handler(bind(&on_open, &c, ::_1));
                c..set_close_handler(bind(on_close, &c, ::_1));

		websocketpp::lib::error_code ec;
		client::connection_ptr con = c.get_connection(uri, ec);
		if (ec) {
			std::cout << "could not create connection because: " << ec.message() << std::endl;
			return 0;
		}

		// Note that connect here only requests a connection. No network messages are
		// exchanged until the event loop starts running in the next line.
		c.connect(con);
                //设置连接断开通知handler
                con->set_termination_handler(bind(on_termination_handler, &c, ::_1));

		// Start the ASIO io_service run loop
		// this will cause a single connection to be made to the server. c.run()
		// will exit when this connection is closed.
		//c.run();

		//线程模式中使用run_one,模拟线程代码如下:
		while (true)
		{
			if (bIsConnectedServer)
			{
				con->send("aaaaa");//文本数据
				con->send("bbbbb", 5, websocketpp::frame::opcode::binary);//字节流
			}

			c.run_one();
			Sleep(500);		
		}
	}
	catch (websocketpp::exception const & e) {
		std::cout << e.what() << std::endl;
	}
        c.stop();
}

主动关闭连接

int closeConnect()
{
	int nRet = 0;
	try {
		if (conn!= NULL && conn->get_state() == websocketpp::session::state::value::open)
		{
			websocketpp::close::status::value cvValue = 0;
			std::string strReason = "";
			conn->close(cvValue, strReason);
		}
	}
	catch (websocketpp::exception const & e)
	{
		nRet = -1;
	}
	return nRet;
}

主动发送数据

int  sendTextData(char *pText)
{
	int nRet = 0;
	try {
		websocketpp::lib::error_code ec;
		ec = conn->send(pText);
		if (ec) {
			std::cout << "Echo failed because: " << ec.message() << std::endl;
			nRet = -1;
		}
	}
	catch (websocketpp::exception const & e) {
		std::cout << e.what() << std::endl;
		return -2;
	}

	return nRet;
}

int sendBinaryData(unsigned char *pData, int nSize)
{
	int nRet = 0;
	try {
		websocketpp::lib::error_code ec;
		ec = conn->send(pData,nSize,websocketpp::frame::opcode::binary);
		if (ec) {
			std::cout << "Echo failed because: " << ec.message() << std::endl;
			nRet = -1;
		}
	}
	catch (websocketpp::exception const & e) {
		std::cout << e.what() << std::endl;
		return -2;
	}

	return nRet;
}

 

### 使用 WebSocketPP 创建 C++ WebSocket 客户端 为了创建基于 WebSocketPPC++ WebSocket 客户端,可以遵循如下结构化的方法来构建应用程序。 #### 初始化库并设置连接选项 首先,在程序启动时初始化 WebSocketPP 库,并配置必要的连接参数: ```cpp #include <websocketpp/config/asio_client.hpp> #include <websocketpp/client.hpp> typedef websocketpp::client<websocketpp::config::asio_tls_client> client; client c; // 设置访问通道日志级别和其他调试信息 c.set_access_channels(websocketpp::log::alevel::all); c.clear_access_channels(websocketpp::log::alevel::frame_payload); // 配置SSL上下文(如果需要TLS) #if defined(ENABLE_SSL) c.init_asio(); c.get_alog().write(websocketpp::log::alevel::app, "Initializing SSL"); #endif ``` #### 注册回调函数处理事件 定义一系列用于响应不同阶段事件的处理器,比如成功建立连接、接收到消息等: ```cpp void on_open(client* c, websocketpp::connection_hdl hdl) { std::cout << "Connection opened" << std::endl; } void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) { std::cout << "Received message: " << msg->get_payload() << std::endl; } ``` 注册这些处理器到客户端实例上: ```cpp c.set_open_handler(bind(&on_open, &c, ::_1)); c.set_message_handler(bind(&on_message, &c, ::_1, ::_2)); ``` #### 建立与服务器之间的连接 指定目标 URI 并尝试发起握手请求: ```cpp websocketpp::lib::error_code ec; client::connection_ptr con = c.get_connection(uri, ec); if (ec) { std::cerr << "Could not create connection because: " << ec.message() << std::endl; return; } con_handle = c.connect(con); // 这里保存hdl以便后续操作 ``` 最后一步是运行 ASIO I/O 服务循环以等待异步调用完成: ```cpp c.run(); // 此处会阻塞直到所有连接关闭 ``` 以上就是使用 WebSocketPP 构建简单 WebSocket 客户端所需的主要步骤[^1]。对于更复杂的应用场景,则可能还需要考虑错误恢复机制、并发控制等方面的内容。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

byxdaz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值