我正在使用 webscokettpp c++ 客户端来读取从网络服务器发布的数据。我有一个带有测试按钮的 ac# UI 应用程序,它调用 c++ 客户端 dll 来获取和显示在套接字上发布的数据。单击测试按钮时,数据第二次在 UI 上正确发布我想关闭现有连接,打开新连接以再次获得结果。但是在第一次运行之后程序永远不会从 c.run(); 返回 在下面的代码中。我正在使用来自https://github.com/zaphoyd/websocketpp/blob/master/examples/echo_client/echo_client.cpp的示例
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <iostream>
typedef websocketpp::client<websocketpp::config::asio_client> client;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
// pull out the type of messages sent by our config
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
// This message handler will be invoked once for each incoming message. It
// prints the message and then sends a copy of the message back to the server.
void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) {
std::cout << "on_message called with hdl: " << hdl.lock().get()
<< " and message: " << msg->get_payload()
<< std::endl;
if(pt.get<std::string>("test") = "close" ){
//Code reaches here after end of first run but never comes out of c.run()
c->close(hdl, websocketpp::close::status::normal, "Success");
hdl.reset();
}
websocketpp::lib::error_code ec;
c->send(hdl, msg->get_payload(), msg->get_opcode(), ec);
if (ec) {
std::cout << "Echo failed because: " << ec.message() << std::endl;
}
}
int main(int argc, char* argv[]) {
// Create a client endpoint
client c;
std::string uri = "ws://localhost:9002";
if (argc == 2) {
uri = argv[1];
}
try {
// Set logging to be pretty verbose (everything except message payloads)
c.set_access_channels(websocketpp::log::alevel::all);
c.clear_access_channels(websocketpp::log::alevel::frame_payload);
// Initialize ASIO
c.init_asio();
// Register our message handler
c.set_message_handler(bind(&on_message,&c,::_1,::_2));
websocketpp::lib::error_code ec;
client::connection_ptr con = c.get_connection(uri, ec);
if (ec) {
std::cout << "could not create connection because: " << ec.message() << std::endl;
return 0;
}
// Note that connect here only requests a connection. No network messages are
// exchanged until the event loop starts running in the next line.
c.connect(con);
// Start the ASIO io_service run loop
// this will cause a single connection to be made to the server. c.run()
// will exit when this connection is closed.
c.run();
} catch (websocketpp::exception const & e) {
std::cout << e.what() << std::endl;
}
}
当条件 if(pt.get("test") = "close") 发生时,我试图找出一种从 c.run() 出来的主要方法的方法。