?WebSocket,URL格式:
WS协议://{主机}:{端口}{RVA PATH},比如:ws://121.40.165.18:8800
协议:WS(HTTP)、WSS(HTTPS),本文则提示 boost::beast::websocket 如何获取 {RVA PATH} 的内容,否则按照?boost::beast::websocket 提供的默认实现,会 accept 任何 RVA_PATH的WS连接请求,按照正常的WS服务器设计是不被允许的。
可用于调试 boost::beast::websocket 服务器端的在线测试网页。
websocket在线测试 (websocket-test.com)
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/beast.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/http/empty_body.hpp>
#include <boost/beast/http/fields.hpp>
#include <boost/beast/http/message.hpp>
#include <boost/beast/http/string_body.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <string>
#include <thread>
using tcp = boost::asio::ip::tcp;
namespace websocket = boost::beast::websocket;
// echoes back all received WebSocket messages
void do_session(tcp::socket& socket)
{
try {
// Construct the stream by moving in the socket
websocket::stream<tcp::socket> ws{ std::move(socket) };
// This buffer is used for reading and must be persisted
boost::beast::flat_buffer buffer;
// Declare a container to hold the response
boost::beast::http::request<boost::beast::http::dynamic_body> req;
// Receive the HTTP response
boost::system::error_code ec;
boost::beast::http::read(ws.next_layer(), buffer, req, ec);
if (ec == boost::beast::http::error::end_of_stream)
ec = boost::beast::websocket::error::closed;
if (ec)
return;
// Accept the websocket handshake
ws.accept(req);
// Print path.
printf("path: %s\n", std::string(req.target().data(), req.target().length()).data());
for (;;) {
// This buffer will hold the incoming message
boost::beast::multi_buffer buffer;
// Read a message
ws.read(buffer);
// Echo the message back
ws.text(ws.got_text());
ws.write(buffer.data());
}
}
catch (boost::system::system_error const& se) {
// This indicates that the session was closed
if (se.code() != websocket::error::closed)
std::cerr << "Error: " << se.code().message() << std::endl;
}
catch (std::exception const& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
}
int main(int argc, char* argv[])
{
try {
// Check command line arguments
if (argc != 3) {
std::cerr <<
"Usage: server <address> <port>\n" <<
"Example:\n" <<
" server 0.0.0.0 8080\n";
return EXIT_FAILURE;
}
auto const address = boost::asio::ip::make_address(argv[1]);
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
// The io_context is required for all I/O
boost::asio::io_context ioc{ 1 };
// The acceptor receives incoming connections
tcp::acceptor acceptor{ ioc, {address, port} };
for (;;) {
// This will receive the new connection
tcp::socket socket{ ioc };
// Block until we get a connection
acceptor.accept(socket);
// Launch the session, transferring ownership of the socket
std::thread{ std::bind(&do_session, std::move(socket)) }.detach();
}
}
catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return EXIT_FAILURE;
}
}
|