VS2015下使用websocketpp和asio构建websock服务器

https://www.zaphoyd.com/websocketpp下载websocketpp,我用的是0.8.1版。

http://think-async.com/下载asio库

这个库是说集成到boost中去了,我也不是说Boost不好,但是确实是在某些情况下Boost会有问题。

这个asio库有Makefile文件,但是我编译后也没看到什么lib库出现,估计都是编译sample去了,而库本身是.h Only的。

 

创建VS2015文件,添加include 路径:{websockpp所在路径}和{asio路径\include}

 

在Preprocessor中添加ASIO_STANDALONE(告诉websocketpp使用asio库);_WEBSOCKETPP_CPP11_INTERNAL_;ASIO_HAS_STD_TYPE_TRAITS;ASIO_HAS_STD_SHARED_PTR;ASIO_HAS_STD_ADDRESSOF;ASIO_HAS_STD_ATOMIC;ASIO_HAS_STD_CHRONO;ASIO_HAS_CSTDINT;ASIO_HAS_STD_ARRAY;ASIO_HAS_STD_SYSTEM_ERROR;(这些都是摆脱Boost依赖所用的)

 

最后的代码如下:

#include <websocketpp/config/asio_no_tls.hpp>

#include <websocketpp/server.hpp>

#include <iostream>
#include <set>

#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;
	}
}

 

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
使用轻量级websocket可以让服务器和客户端之间进行实时通信。在C++中,可以使用第三方来实现websocket通信,如Boost Beast和uWebSockets。 以下是使用Boost Beast实现websocket的简单示例: 1. 安装Boost,并将其包含到项目中。 2. 创建websocket服务器: ```c++ #include <boost/beast/core.hpp> #include <boost/beast/websocket.hpp> #include <boost/asio/ip/tcp.hpp> #include <iostream> #include <string> namespace asio = boost::asio; namespace beast = boost::beast; using tcp = boost::asio::ip::tcp; int main() { asio::io_context ioc{1}; tcp::acceptor acceptor{ioc, {tcp::v4(), 8080}}; while (true) { tcp::socket socket{ioc}; acceptor.accept(socket); try { beast::websocket::stream<tcp::socket> ws{std::move(socket)}; ws.accept(); // 接收客户端消息 beast::flat_buffer buffer; ws.read(buffer); std::string msg = beast::buffers_to_string(buffer.data()); std::cout << "收到消息:" << msg << std::endl; // 发送消息给客户端 ws.write(asio::buffer("服务器收到消息:" + msg)); } catch (const std::exception& e) { std::cerr << "错误:" << e.what() << std::endl; } } return 0; } ``` 3. 创建websocket客户端: ```c++ #include <boost/beast/core.hpp> #include <boost/beast/websocket.hpp> #include <boost/asio/ip/tcp.hpp> #include <iostream> #include <string> namespace asio = boost::asio; namespace beast = boost::beast; using tcp = boost::asio::ip::tcp; int main() { asio::io_context ioc{}; tcp::resolver resolver{ioc}; beast::websocket::stream<tcp::socket> ws{ioc}; // 连接websocket服务器 auto const results = resolver.resolve("localhost", "8080"); asio::connect(ws.next_layer(), results); ws.handshake("localhost", "/"); // 发送消息给服务器 ws.write(asio::buffer("Hello, server!")); // 接收服务器消息 beast::flat_buffer buffer; ws.read(buffer); std::cout << "收到消息:" << beast::buffers_to_string(buffer.data()) << std::endl; return 0; } ``` 在这个示例中,服务器监听8080端口,并在接收到客户端连接后,接收客户端发送的消息,并将其返回。客户端连接服务器并发送一条消息,接收服务器返回的消息。 这只是一个最基本的示例,使用Boost Beast还可以实现更多高级特性,如SSL加密、异步IO等。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值