我从boost :: beast网站复制websocket示例并运行它的Websocket会话工作正常,但我不知道如何将收到的multi_buffer转换为字符串.
下面的代码是websocket会话处理程序.
void
do_session(tcp::socket &socket) {
try {
// Construct the stream by moving in the socket
websocket::stream <tcp::socket> ws{std::move(socket)};
// Accept the websocket handshake
ws.accept();
while (true) {
// This buffer will hold the incoming message
boost::beast::multi_buffer buffer;
// Read a message
boost::beast::error_code ec;
ws.read(buffer, ec);
if (ec == websocket::error::closed) {
break;
}
// Echo the message back
ws.text(ws.got_text());
ws.write(buffer);
}
cout << "Close" << endl;
}
catch (boost::system::system_error const &se) {
// This indicates that the …Run Code Online (Sandbox Code Playgroud) 我是c ++的新手,必须创建用于处理套接字的类.这个类有一个名为listen()的方法,并且在这个方法中需要调用listen函数来实现socket(例如见下)但是方法listen隐藏listen函数如何解决呢?
void CTCPBlockingSocket::listen() {
listen(server_socket,5);
}
Run Code Online (Sandbox Code Playgroud) 我想用类包装类型的引用以向其添加其他功能,如下所示,但我不想强制使用函数或运算符来访问基类型方法。
class A {
private:
int fX;
public:
void SetSomething(int x){ fX = x; }
int GetSomething() { return fX; }
};
template<typename T>
class Ref {
private:
T& fRef;
public:
Ref(T &ref) : fRef(ref) {}
inline operator T&() { return fRef; }
};
int main() {
A a;
Ref<A> ref(a);
ref.SetSomething(100);
return 0;
};
Run Code Online (Sandbox Code Playgroud)
https://godbolt.org/z/8x8aehb8e
可以实现这种模板吗?
c++ ×3
boost ×1
boost-beast ×1
function ×1
listen ×1
methods ×1
reflection ×1
sockets ×1
templates ×1