no message

This commit is contained in:
zcy 2022-02-23 01:35:24 +08:00
parent f458f77e0b
commit 278630d881
2 changed files with 47 additions and 73 deletions

View File

@ -81,7 +81,7 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental> <LinkIncremental>true</LinkIncremental>
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);..\..\;$(ProjectDir)third\include\libevent\include;$(ProjectDir);$(ProjectDir)third\include\lua;$(ProjectDir)third\include\boost171;$(ProjectDir)third\include\</IncludePath> <IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);..\..\;$(ProjectDir)third\include\libevent\include;$(ProjectDir);$(ProjectDir)third\include\lua;$(ProjectDir)third\include\;D:\include</IncludePath>
<ReferencePath>$(ReferencePath)</ReferencePath> <ReferencePath>$(ReferencePath)</ReferencePath>
<LibraryPath>$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(ProjectDir)third\lib</LibraryPath> <LibraryPath>$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(ProjectDir)third\lib</LibraryPath>
</PropertyGroup> </PropertyGroup>
@ -124,7 +124,7 @@
<ClCompile> <ClCompile>
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_WINSOCK_DEPRECATED_NO_WARNINGS;_CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;_WSPIAPI_H_;_WINSOCKAPI_;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>_WEBSOCKETPP_CPP11_TYPE_TRAITS_;_WEBSOCKETPP_CPP11_RANDOM_DEVICE_;ASIO_STANDALONE ;_WINSOCK_DEPRECATED_NO_WARNINGS;_CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;_WSPIAPI_H_;_WINSOCKAPI_;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary> <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile> </ClCompile>

View File

@ -1,6 +1,7 @@
#include "websocket_client.h"
#include "websocketpp/config/asio_no_tls_client.hpp" #include <websocketpp/config/asio_no_tls_client.hpp>
#include "websocketpp/client.hpp" #include <websocketpp/client.hpp>
#include <iostream> #include <iostream>
typedef websocketpp::client<websocketpp::config::asio_client> client; typedef websocketpp::client<websocketpp::config::asio_client> client;
@ -9,90 +10,63 @@ using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2; using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind; using websocketpp::lib::bind;
// pull out the type of messages sent by our config
typedef websocketpp::config::asio_client::message_type::ptr message_ptr; typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
void on_open(client* c, websocketpp::connection_hdl hdl) // 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.
std::string msg = "hello"; void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) {
c->send(hdl, msg, websocketpp::frame::opcode::text);
c->get_alog().write(websocketpp::log::alevel::app, "Tx: " + msg);
}
void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg)
{
std::cout << "on_message called with hdl: " << hdl.lock().get() std::cout << "on_message called with hdl: " << hdl.lock().get()
<< " and message: " << msg->get_payload() << " and message: " << msg->get_payload()
<< std::endl; << std::endl;
websocketpp::lib::error_code ec; websocketpp::lib::error_code ec;
//c->send(hdl,msg->get_payload(),msg->get_opcode(),ec);
if (ec) c->send(hdl, msg->get_payload(), msg->get_opcode(), ec);
{ if (ec) {
std::cout << "Echo failed because " << ec.message() << std::endl; std::cout << "Echo failed because: " << ec.message() << std::endl;
} }
} }
//定时器回调函数 int main11(int argc, char* argv[]) {
void Timeout(client* c, websocketpp::connection_hdl& hdl, boost::asio::deadline_timer* pt, const boost::system::error_code& ec) // Create a client endpoint
{
if (ec)
{
std::cout << "timer is cancel " << std::endl;
return;
}
static int count = 0;
c->send(hdl, "hello", websocketpp::frame::opcode::text);
count++;
if (count > 5)//定时器触发五次后关闭连接
{
c->close(hdl, websocketpp::close::status::normal, "");
return;
}
pt->expires_at(pt->expires_at() + boost::posix_time::seconds(5));
pt->async_wait(bind(Timeout, c, hdl, pt, ::_1));
}
int TestPoint(int argc, char* argv[])
{
client c; client c;
std::string uri = "ws://xx.xx.xx.xx:xxx"; std::string uri = "ws://localhost:9002";
c.set_access_channels(websocketpp::log::alevel::all); if (argc == 2) {
c.clear_access_channels(websocketpp::log::alevel::frame_payload); uri = argv[1];
c.clear_access_channels(websocketpp::log::alevel::frame_header);
// 初始化 ASIO
c.init_asio();
// 注册消息回调
c.set_message_handler(bind(&on_message, &c, ::_1, ::_2));
c.set_open_handler(bind(&on_open, &c, _1));
websocketpp::lib::error_code ec;
client::connection_ptr con = c.get_connection(uri, ec);
con->add_subprotocol("janus-protocol");
if (ec)
{
std::cout << "could not create connection because: " << ec.message() << std::endl;
return 0;
} }
auto hdl = con->get_handle(); try {
c.connect(con); // 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);
boost::asio::deadline_timer t(c.get_io_service(), boost::posix_time::seconds(5)); //设置一个5s超时的定时器 // Initialize ASIO
t.async_wait(bind(&Timeout, &c, hdl, &t, ::_1)); c.init_asio();
std::thread th([&c] { c.run(); }); // Register our message handler
c.set_message_handler(bind(&on_message, &c, ::_1, ::_2));
//休眠13s后取消定时器并关闭连接 websocketpp::lib::error_code ec;
sleep(13); client::connection_ptr con = c.get_connection(uri, ec);
t.cancel(); if (ec) {
c.close(hdl, websocketpp::close::status::normal, ""); std::cout << "could not create connection because: " << ec.message() << std::endl;
return 0;
}
th.join(); // 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;
}
} }