Files
OrcaSlicer/src/slic3r/Utils/WebSocketClient.hpp
T
HanifKoh 30902561c0 Stop Exporting Names Through Usings in the Remaining Headers (#16250)
The last headers with a using or namespace alias at namespace scope:

- TCPConsole.hpp imported boost::asio::ip::tcp into Slic3r::Utils for
  two member declarations. The alias is now a private member of the
  class.
- WebSocketClient.hpp declared four namespace aliases and a tcp alias
  at global scope, each used only by the header. The names are spelled
  out.
- Repair.hpp aliased CGAL::Polygon_mesh_processing as PMP in
  Slic3r::tex2color. The three functions that use it declare the alias
  themselves.
- PreciseSeam.hpp, Thumbnails.hpp and MarchingSquares.hpp used a
  using-declaration or directive for one or two spots each; those spots
  are qualified. Thumbnails.hpp's "PNG"sv default argument becomes
  "PNG", which converts to the std::string_view parameter the same way.
- tests/sla_print/sla_test_utils.hpp had "using namespace Slic3r;" and
  tests/filament_group/fg_test_serialization.hpp "using json =
  nlohmann::json;" at global scope. The headers qualify their own names;
  the two SLA test sources get the directive themselves.

Also removed: twelve type aliases in headers that nothing references
(ConflictObjName, CircleSqf, CircleSqd, TRawBuffer, DistanceFunction,
SamePair, ExtruderNozzleInfos, Vec2dEvent, Vec2dsEvent, Vec3dEvent,
t_option, t_optgroups, Plater::fs_path) and a duplicate
fn_ft_job_msg_destroy alias in FileTransferUtils.hpp.
2026-10-08 13:59:12 +08:00

105 lines
3.2 KiB
C++

#ifndef _WEB_SOCKET_CLIENT_HPP_
#define _WEB_SOCKET_CLIENT_HPP_
#include <boost/asio/buffer.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http/field.hpp>
#include <boost/beast/core/flat_buffer.hpp>
#include <boost/beast/core/buffers_to_string.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/beast/websocket/rfc6455.hpp>
#include <exception>
#include <boost/beast/websocket/stream_base.hpp>
#include <boost/beast/websocket/stream.hpp>
#include <iostream>
#include <string>
#include <chrono>
class WebSocketClient {
public:
//服务器是ws://echo.websocket.org:80/websocket
WebSocketClient():
resolver_(ioc_), ws_(ioc_),is_connect(false) {
}
~WebSocketClient() {
if(!is_connect){
return;
}
try {
// Close the WebSocket connection
ws_.close(boost::beast::websocket::close_code::normal);
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
}
void connect(const std::string& host, const std::string& port, const std::string& path="/"){
if(is_connect){
return;
}
// Look up the domain name
auto const results = resolver_.resolve(host, port);
// Make the connection on the IP address we get from a lookup
auto ep = boost::asio::connect(ws_.next_layer(), results);
std::string _host = host;
//if _host last char is '/', remove it
if(_host.size()>0&&_host[host.size()-1] == '/'){
_host[host.size()-1] = '\0';
}
// _host += ':' + std::to_string(ep.port());
// Set a decorator to change the User-Agent of the handshake
ws_.set_option(boost::beast::websocket::stream_base::decorator(
[](boost::beast::websocket::request_type& req)
{
req.set(boost::beast::http::field::user_agent,"ElegooSlicer");
}));
// Perform the WebSocket handshake
ws_.handshake(_host, path);
is_connect = true;
}
void send(const std::string& message){
// Send a message
ws_.write(boost::asio::buffer(message));
}
std::string receive(int timeout = 0){
// This buffer will hold the incoming message
boost::beast::flat_buffer buffer;
// Read a message into our buffer
ws_.read(buffer);
// Return the message as a string
return boost::beast::buffers_to_string(buffer.data());
}
private:
boost::asio::io_context ioc_;
boost::asio::ip::tcp::resolver resolver_;
boost::beast::websocket::stream<boost::asio::ip::tcp::socket> ws_;
bool is_connect;
};
// int main() {
// try {
// WebSocketClient client("echo.websocket.org", "80");
// client.send("Hello, world!");
// std::string response = client.receive();
// std::cout << "Received: " << response << std::endl;
// } catch (const std::exception& e) {
// std::cerr << "Error: " << e.what() << std::endl;
// return EXIT_FAILURE;
// }
// return EXIT_SUCCESS;
// }
#endif // _WEB_SOCKET_CLIENT_HPP