websocketpp最简单的服务器

Posted qianbo_insist

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了websocketpp最简单的服务器相关的知识,希望对你有一定的参考价值。

预定义

1 WEBSOCKETPP_CPP11_TYPE_TRAITS
2 ASIO_STANDALONE;
3 SCL_SECURE_NO_WARNINGS;
A 不要使用太新的版本
B 不使用boost 就把ASIO_STANDALONE 预定义加上,使用boost库毕竟比较庞大。

set_access_channels 函数定义输出的日志,输出日志太多效率就低了,这个一定要知道。

#define _SCL_SECURE_NO_WARNINGS
#define WEBSOCKETPP_CPP11_TYPE_TRAITS

code 1

#define _SCL_SECURE_NO_WARNINGS
#define _WEBSOCKETPP_CPP11_TYPE_TRAITS_
#define  ASIO_STANDALONE
#include <websocketpp/config/core.hpp>
#include <websocketpp/server.hpp>
#include <iostream>
#include <fstream>


#if 1
typedef websocketpp::server<websocketpp::config::core> server;

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 server::message_ptr message_ptr;

// Define a callback to handle incoming messages
void on_message(server* s, websocketpp::connection_hdl hdl, message_ptr msg) {
	if (msg->get_opcode() == websocketpp::frame::opcode::text) {
		s->get_alog().write(websocketpp::log::alevel::app,
			"Text Message Received: " + msg->get_payload());
	}
	else {
		s->get_alog().write(websocketpp::log::alevel::app,
			"Binary Message Received: " + websocketpp::utility::to_hex(msg->get_payload()));
	}

	try {
		s->send(hdl, msg->get_payload(), msg->get_opcode());
	}
	catch (websocketpp::exception const & e) {
		s->get_alog().write(websocketpp::log::alevel::app,
			std::string("Echo Failed: ") + e.what());
	}
}

int main() {
	server s;
	std::ofstream log;

	try {
		// set up access channels to only log interesting things
		s.clear_access_channels(websocketpp::log::alevel::all);
		s.set_access_channels(websocketpp::log::alevel::connect);
		s.set_access_channels(websocketpp::log::alevel::disconnect);
		s.set_access_channels(websocketpp::log::alevel::app);

		// Log to a file rather than stdout, as we are using stdout for real
		// output
		log.open("output.log");
		s.get_alog().set_ostream(&log);
		s.get_elog().set_ostream(&log);

		// print all output to stdout
		s.register_ostream(&std::cout);

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

		server::connection_ptr con = s.get_connection();

		con->start();

		// C++ iostream's don't support the idea of asynchronous i/o. As such
		// there are two input strategies demonstrated here. Buffered I/O will
		// read from stdin in chunks until EOF. This works very well for
		// replaying canned connections as would be done in automated testing.
		//
		// If the server is being used live however, assuming input is being
		// piped from elsewhere in realtime, this strategy will result in small
		// messages being buffered forever. The non-buffered strategy below
		// reads characters from stdin one at a time. This is inefficient and
		// for more serious uses should be replaced with a platform specific
		// asyncronous i/o technique like select, poll, IOCP, etc
		bool buffered_io = false;

		if (buffered_io) {
			std::cin >> *con;
			con->eof();
		}
		else {
			char a;
			while (std::cin.get(a)) {
				con->read_some(&a, 1);
			}
			con->eof();
		}
	}
	catch (websocketpp::exception const & e) {
		std::cout << e.what() << std::endl;
	}
	log.close();
}

#endif

code 2

#if 1
#include <set>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.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;

class broadcast_server {
public:
	broadcast_server() {
		m_server.init_asio();

		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 on_open(connection_hdl hdl) {
		m_connections.insert(hdl);
	}

	void on_close(connection_hdl hdl) {
		m_connections.erase(hdl);
	}

	void on_message(connection_hdl hdl, server::message_ptr msg) {
		for (auto it : m_connections) {
			m_server.send(it, msg);
		}
	}

	void run(uint16_t port) {
		m_server.listen(port);
		m_server.start_accept();
		m_server.run();
	}
private:
	typedef std::set<connection_hdl, std::owner_less<connection_hdl>> con_list;

	server m_server;
	con_list m_connections;
};

int main() {
	broadcast_server server;
	std::cout << "server running on 9002" << std::endl;
	server.run(9002);

}
#endif

以上是关于websocketpp最简单的服务器的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 websocket websocketpp 发送和接收消息?

代码片段 - Golang 实现简单的 Web 服务器

Qt写websocketpp服务端

处理websocketpp连接路径

使用 websocketpp 时“调用绑定没有匹配的函数”

如何在 websocketpp 中使用自定义记录器?