中间件底层,websocket

CWsToolDlg.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // CWsToolDlg.cpp: 实现文件
  2. //
  3. #include "pch.h"
  4. #include "SoftTool.h"
  5. #include "CWsToolDlg.h"
  6. #include "afxdialogex.h"
  7. // CWsToolDlg 对话框
  8. IMPLEMENT_DYNAMIC(CWsToolDlg, CDialogEx)
  9. CWsToolDlg::CWsToolDlg(CWnd* pParent /*=nullptr*/)
  10. : CDialogEx(IDD_WSTOOL_DIALOG, pParent)
  11. {
  12. }
  13. CWsToolDlg::~CWsToolDlg()
  14. {
  15. try
  16. {
  17. if (!ios.stopped())
  18. ios.stop();
  19. if (thread_ && thread_->joinable())
  20. {
  21. thread_->join();
  22. }
  23. }
  24. catch (const std::exception& e)
  25. {
  26. std::cout << e.what() << std::endl;
  27. }
  28. }
  29. void CWsToolDlg::DoDataExchange(CDataExchange* pDX)
  30. {
  31. CDialogEx::DoDataExchange(pDX);
  32. DDX_Control(pDX, IDC_EDIT1, m_eWsUri);
  33. DDX_Control(pDX, IDC_EDIT2, m_eSoftDayNum);
  34. DDX_Control(pDX, IDC_LIST1, m_lbRecvMsg);
  35. }
  36. BEGIN_MESSAGE_MAP(CWsToolDlg, CDialogEx)
  37. ON_BN_CLICKED(IDC_BUTTON1, &CWsToolDlg::OnBnClickedButton1)
  38. ON_BN_CLICKED(IDC_BUTTON2, &CWsToolDlg::OnBnClickedButton2)
  39. ON_BN_CLICKED(IDC_BUTTON3, &CWsToolDlg::OnBnClickedButton3)
  40. END_MESSAGE_MAP()
  41. // CWsToolDlg 消息处理程序
  42. BOOL CWsToolDlg::OnInitDialog()
  43. {
  44. CDialogEx::OnInitDialog();
  45. // TODO: 在此添加额外的初始化
  46. m_eWsUri.SetWindowText("ws://127.0.0.1:8081");
  47. m_eSoftDayNum.SetWindowText("0");
  48. GetDlgItem(IDC_BUTTON1)->EnableWindow(true);
  49. GetDlgItem(IDC_BUTTON2)->EnableWindow(false);
  50. c.set_access_channels(websocketpp::log::alevel::none ^ websocketpp::log::alevel::none);
  51. c.set_error_channels(websocketpp::log::alevel::none ^ websocketpp::log::alevel::none);
  52. c.init_asio(&ios);
  53. c.set_message_handler(websocketpp::lib::bind(&this_type::on_message, this, ::_1, ::_2));
  54. c.set_fail_handler(websocketpp::lib::bind(&this_type::on_fail, this, ::_1));
  55. c.set_open_handler(websocketpp::lib::bind(&this_type::on_open, this, ::_1));
  56. c.set_close_handler(websocketpp::lib::bind(&this_type::on_close, this, ::_1));
  57. c.set_reuse_addr(true);
  58. c.start_perpetual();
  59. //if (thread_ == nullptr)
  60. thread_ = websocketpp::lib::make_shared<websocketpp::lib::thread>(boost::bind(&boost::asio::io_service::run, &ios));
  61. return TRUE; // return TRUE unless you set the focus to a control
  62. // 异常: OCX 属性页应返回 FALSE
  63. }
  64. // 连接
  65. void CWsToolDlg::OnBnClickedButton1()
  66. {
  67. // TODO: 在此添加控件通知处理程序代码
  68. CString str;
  69. m_eWsUri.GetWindowText(str);
  70. this->m_strUri = str.GetBuffer(0);
  71. websocketpp::lib::error_code ec;
  72. client::connection_ptr con = c.get_connection(this->m_strUri, ec);
  73. if (ec)
  74. {
  75. // LOG_DEBUG("ws连接失败,失败信息[%s]",ec.message().c_str());
  76. std::cout << "could not create connection because: " << ec.message() << std::endl;
  77. return ;
  78. }
  79. hdl_ = con->get_handle();
  80. c.connect(con);
  81. }
  82. // 关闭
  83. void CWsToolDlg::OnBnClickedButton2()
  84. {
  85. // TODO: 在此添加控件通知处理程序代码
  86. try
  87. {
  88. if (!hdl_.expired())
  89. c.close(hdl_, websocketpp::close::status::normal, "");
  90. }
  91. catch (const websocketpp::exception &e)
  92. {
  93. std::cout << e.what() << std::endl;
  94. }
  95. }
  96. // 授权
  97. void CWsToolDlg::OnBnClickedButton3()
  98. {
  99. // TODO: 在此添加控件通知处理程序代码
  100. CString str;
  101. m_eSoftDayNum.GetWindowText(str);
  102. Json::Value root;
  103. root["Type"] = "SoftAuth";
  104. root["DayNum"] = str.GetBuffer(0);
  105. Json::StreamWriterBuilder jswBuilder;
  106. jswBuilder["emitUTF8"] = true;
  107. //std::unique_ptr<Json::StreamWriter>jsWriter(jswBuilder.newStreamWriter());
  108. std::string strJson = Json::writeString(jswBuilder, root);
  109. sendMsg(strJson);
  110. }
  111. bool CWsToolDlg::sendMsg(const std::string & msg)
  112. {
  113. try
  114. {
  115. c.send(hdl_, msg, websocketpp::frame::opcode::TEXT);
  116. return true;
  117. }
  118. catch (websocketpp::exception const &e)
  119. {
  120. CString str;
  121. str.Format("发送失败:\n%s",e.what());
  122. AfxMessageBox(str);
  123. return false;
  124. }
  125. return false;
  126. }
  127. void CWsToolDlg::on_open(websocketpp::connection_hdl hdl)
  128. {
  129. GetDlgItem(IDC_BUTTON1)->EnableWindow(false);
  130. GetDlgItem(IDC_BUTTON2)->EnableWindow(true);
  131. }
  132. void CWsToolDlg::on_fail(websocketpp::connection_hdl hdl)
  133. {
  134. GetDlgItem(IDC_BUTTON2)->EnableWindow(false);
  135. AfxMessageBox("连接失败");
  136. }
  137. void CWsToolDlg::on_close(websocketpp::connection_hdl hdl)
  138. {
  139. GetDlgItem(IDC_BUTTON1)->EnableWindow(true);
  140. GetDlgItem(IDC_BUTTON2)->EnableWindow(false);
  141. }
  142. void CWsToolDlg::on_message(websocketpp::connection_hdl hdl, message_ptr msg)
  143. {
  144. m_lbRecvMsg.InsertString(0,msg->get_payload().c_str());
  145. }