diff --git a/examples/proto_debuger/proto_debuger.vcxproj b/examples/proto_debuger/proto_debuger.vcxproj index 0c0ccbcb..824413c1 100644 --- a/examples/proto_debuger/proto_debuger.vcxproj +++ b/examples/proto_debuger/proto_debuger.vcxproj @@ -81,7 +81,7 @@ true - $(VC_IncludePath);$(WindowsSDK_IncludePath);..\..\;$(ProjectDir)third\include\libevent\include;$(ProjectDir);$(ProjectDir)third\include\lua;$(ProjectDir)third\include\boost171;$(ProjectDir)third\include\ + $(VC_IncludePath);$(WindowsSDK_IncludePath);..\..\;$(ProjectDir)third\include\libevent\include;$(ProjectDir);$(ProjectDir)third\include\lua;$(ProjectDir)third\include\;D:\include $(ReferencePath) $(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(ProjectDir)third\lib @@ -124,7 +124,7 @@ Level3 true - _WINSOCK_DEPRECATED_NO_WARNINGS;_CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;_WSPIAPI_H_;_WINSOCKAPI_;%(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) true MultiThreadedDebug diff --git a/examples/proto_debuger/websocket_client.cpp b/examples/proto_debuger/websocket_client.cpp index af0e1a67..90c5807c 100644 --- a/examples/proto_debuger/websocket_client.cpp +++ b/examples/proto_debuger/websocket_client.cpp @@ -1,6 +1,7 @@ -#include "websocket_client.h" -#include "websocketpp/config/asio_no_tls_client.hpp" -#include "websocketpp/client.hpp" + +#include +#include + #include typedef websocketpp::client client; @@ -9,90 +10,63 @@ 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; -void on_open(client* c, websocketpp::connection_hdl hdl) -{ - std::string msg = "hello"; - 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) -{ +// 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; + + 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; + + c->send(hdl, msg->get_payload(), msg->get_opcode(), ec); + if (ec) { + std::cout << "Echo failed because: " << ec.message() << std::endl; } } -//定时器回调函数 -void Timeout(client* c, websocketpp::connection_hdl& hdl, boost::asio::deadline_timer* pt, const boost::system::error_code& ec) -{ - 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[]) -{ +int main11(int argc, char* argv[]) { + // Create a client endpoint 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); - c.clear_access_channels(websocketpp::log::alevel::frame_payload); - 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; + if (argc == 2) { + uri = argv[1]; } - auto hdl = con->get_handle(); - c.connect(con); + 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); - boost::asio::deadline_timer t(c.get_io_service(), boost::posix_time::seconds(5)); //设置一个5s超时的定时器 - t.async_wait(bind(&Timeout, &c, hdl, &t, ::_1)); + // Initialize ASIO + c.init_asio(); - std::thread th([&c] { c.run(); }); + // Register our message handler + c.set_message_handler(bind(&on_message, &c, ::_1, ::_2)); - //休眠13s后取消定时器并关闭连接 - sleep(13); - t.cancel(); - c.close(hdl, websocketpp::close::status::normal, ""); + 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; + } - 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; + } } \ No newline at end of file