【发布时间】:2019-06-25 15:51:35
【问题描述】:
我想随时send 消息。但是,我不知道将send() 放在哪里。我实际上不知道在调用 run() 而不是在处理程序内部之后会发生什么。
我目前的设置只是在send 我在on_open 中的消息,因为这是我唯一可以输入send 以使其工作的地方。但是,以这种方式发送多条消息被证明是一个问题。我可以关闭来自on_message 的连接,但我无法通过该客户端再次send,无论我是再次connect 还是run()。
理想的解决方案是不必close 并重新打开连接或端点。同样,我想知道如果不在on_open 或on_message 中,我应该把send 放在哪里
c.init_asio();
c.start_perpetual();//I've tried both perpetual and non-perpetual
//[associate handlers]
c.connect(con); //connecting and running works fine;
c.run(); //on_open, on_message work fine
sleep(15); //is 15 seconds enough for the asio to clean up everything?
c.connect(con); //any permutation of these two lines
c.run(); //do nothing after close() has been called
std::string test("test");
con->send(test,websocketpp::frame::opcode::TEXT); //this does nothing
使用 C++11 和 VS2015。另外我无法复制/粘贴代码,所以我可能错过了一些愚蠢的错别字。不知道还能说什么。我不知道我可以把 send 放在哪里,以便我可以在按下按钮时发送它,例如
【问题讨论】:
-
你的
c.run();包裹io_context.run()吗? -
如果是这样......
c.run(); //on_open, on_message work finesleep(15); //is 15 seconds enough for the asio to clean up everything?尝试在此处重置 io_context.reset()。c.connect(con);c.run(); -
从这一行
init_asio(),我假设您正在使用 boost.asio,而您的问题与 boost.asio 有关。 -
@Explorer_N 它说“包装了内部 io_service 对象的运行方法”。这不是我的
run(),而是 websocket++ 的run()。我尝试在sleep之前使用c.reset(),它确实说“连接成功”,但没有其他任何反应。我重新关联了处理程序,但它们第二次没有激活。 -
websocket++。听起来像图书馆。 “我尝试在睡眠前使用c.reset()”它必须在
sleep之后和c.connect(con)之前,首先,你为什么需要sleep和多个run?
标签: websocket++