boost websocket ssl简单示例

最近开发websocket和页面端进行通信,https连接需要wss,需要boost websocket sll。boost库中有类似示例,本例是在示例基础上进行修改。主要是使用自己证书。

关键代码如下:

auto const address = net::ip::make_address("127.0.0.1");
		auto const port = static_cast<unsigned short>(8080);

		// The io_context is required for all I/O
		net::io_context ioc{ 1 };

		ssl::context ctx{ ssl::context::tlsv12 };


		ctx.use_certificate_chain_file("self.pem");
		ctx.use_private_key_file("self.key", boost::asio::ssl::context::pem);
		// The acceptor receives incoming connections
		tcp::acceptor acceptor{ ioc, {address, port} };

 

在 OpenWrt 上编写 WebSocket 服务器需要使用 C/C++ 或其他语言,并使用相应的库来实现 WebSocket 协议。以下是一个使用 C++ 和 Boost.Asio 库编写的简单 WebSocket 服务器示例: 1. 首先,安装必要的软件包,包括 boost,libopenssl 和 libpthread: ``` opkg update opkg install libboost-system libboost-filesystem libopenssl libpthread ``` 2. 创建一个名为 `websocket_server.cpp` 的文件,并将以下代码复制到其中: ```c++ #include <iostream> #include <string> #include <boost/asio.hpp> #include <boost/asio/ssl.hpp> #include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <boost/enable_shared_from_this.hpp> #include <boost/algorithm/string.hpp> using boost::asio::ip::tcp; class websocket_session : public boost::enable_shared_from_this<websocket_session> { public: websocket_session(boost::asio::io_service& io_service, boost::asio::ssl::context& context) : socket_(io_service, context) { } tcp::socket& socket() { return socket_.next_layer(); } void start() { socket_.async_handshake(boost::asio::ssl::stream_base::server, boost::bind(&websocket_session::handle_handshake, shared_from_this(), boost::asio::placeholders::error)); } private: void handle_handshake(const boost::system::error_code& error) { if (!error) { boost::asio::async_read_until(socket_, request_, "\r\n\r\n", boost::bind(&websocket_session::handle_request, shared_from_this(), boost::asio::placeholders::error)); } } void handle_request(const boost::system::error_code& error) { if (!error) { std::istream request_stream(&request_); std::string header; while (std::getline(request_stream, header) && header != "\r") { std::vector<std::string> tokens; boost::split(tokens, header, boost::is_any_of(":")); if (tokens.size() == 2 && tokens[0] == "Sec-WebSocket-Key") { std::string key = tokens[1] + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; std::string sha1_key = boost::asio::detail::sha1(key); std::string accept_key = boost::asio::detail::base64_encode( reinterpret_cast<const unsigned char*>(sha1_key.c_str()), sha1_key.length()); boost::asio::streambuf response; std::ostream response_stream(&response); response_stream << "HTTP/1.1 101 Switching Protocols\r\n" << "Upgrade: websocket\r\n" << "Connection: Upgrade\r\n" << "Sec-WebSocket-Accept: " << accept_key << "\r\n\r\n"; boost::asio::async_write(socket_, response, boost::bind(&websocket_session::handle_write, shared_from_this(), boost::asio::placeholders::error)); return; } } } } void handle_write(const boost::system::error_code& error) { if (!error) { boost::asio::async_read(socket_, boost::asio::buffer(buffer_), boost::asio::transfer_at_least(1), boost::bind(&websocket_session::handle_read, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } } void handle_read(const boost::system::error_code& error, std::size_t bytes_transferred) { if (!error) { std::ostream response_stream(&response_); response_stream << "HTTP/1.1 200 OK\r\n" << "Content-Type: text/plain\r\n" << "Content-Length: " << bytes_transferred << "\r\n\r\n"; response_stream.write(buffer_.data(), bytes_transferred); boost::asio::async_write(socket_, response_, boost::bind(&websocket_session::handle_write, shared_from_this(), boost::asio::placeholders::error)); } } boost::asio::ssl::stream<tcp::socket> socket_; boost::asio::streambuf request_; boost::asio::streambuf response_; boost::array<char, 8192> buffer_; }; class websocket_server { public: websocket_server(boost::asio::io_service& io_service, unsigned short port) : io_service_(io_service), acceptor_(io_service, tcp::endpoint(tcp::v4(), port)), context_(boost::asio::ssl::context::sslv23) { context_.set_options(boost::asio::ssl::context::default_workarounds | boost::asio::ssl::context::no_sslv2 | boost::asio::ssl::context::single_dh_use); context_.set_password_callback(boost::bind(&websocket_server::get_password, this)); context_.use_certificate_chain_file("server.crt"); context_.use_private_key_file("server.key", boost::asio::ssl::context::pem); start_accept(); } std::string get_password() const { return "test"; } private: void start_accept() { websocket_session::pointer new_session = websocket_session::create(io_service_, context_); acceptor_.async_accept(new_session->socket(), boost::bind(&websocket_server::handle_accept, this, new_session, boost::asio::placeholders::error)); } void handle_accept(websocket_session::pointer new_session, const boost::system::error_code& error) { if (!error) { new_session->start(); start_accept(); } } boost::asio::io_service& io_service_; tcp::acceptor acceptor_; boost::asio::ssl::context context_; }; int main(int argc, char* argv[]) { try { if (argc != 2) { std::cerr << "Usage: websocket_server <port>\n"; return 1; } boost::asio::io_service io_service; websocket_server server(io_service, std::atoi(argv[1])); io_service.run(); } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << "\n"; } return 0; } ``` 3. 编译代码: ``` g++ -o websocket_server websocket_server.cpp -lboost_system -lboost_filesystem -lboost_thread -lssl -lcrypto -lpthread ``` 4. 运行服务器: ``` ./websocket_server 8080 ``` 现在,WebSocket 服务器已经在 OpenWrt 上启动,并监听端口 8080。您可以使用 WebSocket 客户端连接到该服务器并进行交互。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值