官方的example中仅仅输出到控制台,而且不支持中文,这里我加入了ansi到utf8的转换,使用utf8就能正常解析中文。
server:
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <cstdlib>
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>
namespace beast = boost::beast;
namespace http = beast::http;
namespace websocket = beast::websocket;
namespace net = boost::asio;
using tcp = boost::asio::ip::tcp;
std::wstring string_to_wstring(const std::string &s)
{
using default_convert = std::codecvt<wchar_t, char, std::mbstate_t>;
static std::wstring_convert<default_convert>conv(new default_convert("CHS"));
return conv.from_bytes(s);
}
std::string wstring_to_string(const std::wstring &s)
{
using default_convert = std::codecvt<wchar_t, char, std::mbstate_t>;
static std::wstring_convert<default_convert>conv(new default_convert("CHS"));
return conv.to_bytes(s);
}
std::string ansi_to_utf8(const std::string &s)
{
static std::wstring_convert<std::codecvt_utf8<wchar_t> > conv;
return conv.to_bytes(string_to_wstring(s));
}
std::string utf8_to_ansi(const std::string& s)
{
static std::wstring_convert<std::codecvt_utf8<wchar_t> > conv;
return wstring_to_string(conv.from_bytes(s));
}
int main(int argc, char** argv)
{
try
{
net::io_context ioc;
tcp::resolver resolver{ ioc };
websocket::stream<tcp::socket> ws{ ioc };
auto const address = net::ip::make_address("192.168.0.27");
auto const port = static_cast<unsigned short>(std::atoi("80"));
tcp::endpoint endpoint{ address, port };
auto const results = resolver.resolve(endpoint);
net::connect(ws.next_layer(), results.begin(), results.end());
ws.set_option(websocket::stream_base::decorator(
[](websocket::request_type& req)
{
req.set(http::field::user_agent,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-client-coro");
}));
ws.handshake("0.0.0.0", "/");
while (true)
{
std::string text;
std::cin >> text;
ws.write(net::buffer(ansi_to_utf8(text)));
beast::flat_buffer buffer;
ws.read(buffer);
std::string out;
out = beast::buffers_to_string(buffer.cdata());
std::cout << utf8_to_ansi(out) << std::endl;
}
ws.close(websocket::close_code::normal);
}
catch (std::exception const& e)
{
std::cerr << "Error: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
client:
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <string>
#include <thread>
#include <codecvt>
namespace beast = boost::beast;
namespace http = beast::http;
namespace websocket = beast::websocket;
namespace net = boost::asio;
using tcp = boost::asio::ip::tcp;
std::wstring string_to_wstring(const std::string& s)
{
using default_convert = std::codecvt<wchar_t, char, std::mbstate_t>;
static std::wstring_convert<default_convert>conv(new default_convert("CHS"));
return conv.from_bytes(s);
}
std::string wstring_to_string(const std::wstring& s)
{
using default_convert = std::codecvt<wchar_t, char, std::mbstate_t>;
static std::wstring_convert<default_convert>conv(new default_convert("CHS"));
return conv.to_bytes(s);
}
std::string ansi_to_utf8(const std::string& s)
{
static std::wstring_convert<std::codecvt_utf8<wchar_t> > conv;
return conv.to_bytes(string_to_wstring(s));
}
std::string utf8_to_ansi(const std::string& s)
{
static std::wstring_convert<std::codecvt_utf8<wchar_t> > conv;
return wstring_to_string(conv.from_bytes(s));
}
void
do_session(tcp::socket& socket)
{
try
{
websocket::stream<tcp::socket> ws{ std::move(socket) };
ws.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res)
{
res.set(http::field::server,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-server-sync");
}));
ws.accept();
for (;;)
{
beast::flat_buffer buffer;
ws.read(buffer);
auto out = beast::buffers_to_string(buffer.cdata());
std::cout << utf8_to_ansi(out) << std::endl;
ws.text(ws.got_text());
ws.write(buffer.data());
}
}
catch (beast::system_error const& se)
{
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
{
auto const address = net::ip::make_address("0.0.0.0");
auto const port = static_cast<unsigned short>(std::atoi("80"));
net::io_context ioc{ 1 };
tcp::acceptor acceptor{ ioc, {address, port} };
for (;;)
{
tcp::socket socket{ ioc };
acceptor.accept(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;
}
}