| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- // CWsToolDlg.cpp: 实现文件
- //
- #include "pch.h"
- #include "SoftTool.h"
- #include "CWsToolDlg.h"
- #include "afxdialogex.h"
- // CWsToolDlg 对话框
- IMPLEMENT_DYNAMIC(CWsToolDlg, CDialogEx)
- CWsToolDlg::CWsToolDlg(CWnd* pParent /*=nullptr*/)
- : CDialogEx(IDD_WSTOOL_DIALOG, pParent)
- {
- }
- CWsToolDlg::~CWsToolDlg()
- {
- try
- {
- if (!ios.stopped())
- ios.stop();
- if (thread_ && thread_->joinable())
- {
- thread_->join();
- }
- }
- catch (const std::exception& e)
- {
- std::cout << e.what() << std::endl;
- }
- }
- void CWsToolDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialogEx::DoDataExchange(pDX);
- DDX_Control(pDX, IDC_EDIT1, m_eWsUri);
- DDX_Control(pDX, IDC_EDIT2, m_eSoftDayNum);
- DDX_Control(pDX, IDC_LIST1, m_lbRecvMsg);
- }
- BEGIN_MESSAGE_MAP(CWsToolDlg, CDialogEx)
- ON_BN_CLICKED(IDC_BUTTON1, &CWsToolDlg::OnBnClickedButton1)
- ON_BN_CLICKED(IDC_BUTTON2, &CWsToolDlg::OnBnClickedButton2)
- ON_BN_CLICKED(IDC_BUTTON3, &CWsToolDlg::OnBnClickedButton3)
- END_MESSAGE_MAP()
- // CWsToolDlg 消息处理程序
- BOOL CWsToolDlg::OnInitDialog()
- {
- CDialogEx::OnInitDialog();
- // TODO: 在此添加额外的初始化
- m_eWsUri.SetWindowText("ws://127.0.0.1:8081");
- m_eSoftDayNum.SetWindowText("0");
- GetDlgItem(IDC_BUTTON1)->EnableWindow(true);
- GetDlgItem(IDC_BUTTON2)->EnableWindow(false);
- c.set_access_channels(websocketpp::log::alevel::none ^ websocketpp::log::alevel::none);
- c.set_error_channels(websocketpp::log::alevel::none ^ websocketpp::log::alevel::none);
- c.init_asio(&ios);
- c.set_message_handler(websocketpp::lib::bind(&this_type::on_message, this, ::_1, ::_2));
- c.set_fail_handler(websocketpp::lib::bind(&this_type::on_fail, this, ::_1));
- c.set_open_handler(websocketpp::lib::bind(&this_type::on_open, this, ::_1));
- c.set_close_handler(websocketpp::lib::bind(&this_type::on_close, this, ::_1));
- c.set_reuse_addr(true);
- c.start_perpetual();
- //if (thread_ == nullptr)
- thread_ = websocketpp::lib::make_shared<websocketpp::lib::thread>(boost::bind(&boost::asio::io_service::run, &ios));
- return TRUE; // return TRUE unless you set the focus to a control
- // 异常: OCX 属性页应返回 FALSE
- }
- // 连接
- void CWsToolDlg::OnBnClickedButton1()
- {
- // TODO: 在此添加控件通知处理程序代码
- CString str;
- m_eWsUri.GetWindowText(str);
- this->m_strUri = str.GetBuffer(0);
- websocketpp::lib::error_code ec;
- client::connection_ptr con = c.get_connection(this->m_strUri, ec);
- if (ec)
- {
- // LOG_DEBUG("ws连接失败,失败信息[%s]",ec.message().c_str());
- std::cout << "could not create connection because: " << ec.message() << std::endl;
- return ;
- }
- hdl_ = con->get_handle();
- c.connect(con);
- }
- // 关闭
- void CWsToolDlg::OnBnClickedButton2()
- {
- // TODO: 在此添加控件通知处理程序代码
- try
- {
- if (!hdl_.expired())
- c.close(hdl_, websocketpp::close::status::normal, "");
- }
- catch (const websocketpp::exception &e)
- {
- std::cout << e.what() << std::endl;
- }
- }
- // 授权
- void CWsToolDlg::OnBnClickedButton3()
- {
- // TODO: 在此添加控件通知处理程序代码
- CString str;
- m_eSoftDayNum.GetWindowText(str);
- Json::Value root;
- root["Type"] = "SoftAuth";
- root["DayNum"] = str.GetBuffer(0);
- Json::StreamWriterBuilder jswBuilder;
- jswBuilder["emitUTF8"] = true;
- //std::unique_ptr<Json::StreamWriter>jsWriter(jswBuilder.newStreamWriter());
- std::string strJson = Json::writeString(jswBuilder, root);
- sendMsg(strJson);
- }
- bool CWsToolDlg::sendMsg(const std::string & msg)
- {
- try
- {
- c.send(hdl_, msg, websocketpp::frame::opcode::TEXT);
- return true;
- }
- catch (websocketpp::exception const &e)
- {
- CString str;
- str.Format("发送失败:\n%s",e.what());
- AfxMessageBox(str);
- return false;
- }
- return false;
- }
- void CWsToolDlg::on_open(websocketpp::connection_hdl hdl)
- {
- GetDlgItem(IDC_BUTTON1)->EnableWindow(false);
- GetDlgItem(IDC_BUTTON2)->EnableWindow(true);
- }
- void CWsToolDlg::on_fail(websocketpp::connection_hdl hdl)
- {
- GetDlgItem(IDC_BUTTON2)->EnableWindow(false);
- AfxMessageBox("连接失败");
- }
- void CWsToolDlg::on_close(websocketpp::connection_hdl hdl)
- {
- GetDlgItem(IDC_BUTTON1)->EnableWindow(true);
- GetDlgItem(IDC_BUTTON2)->EnableWindow(false);
- }
- void CWsToolDlg::on_message(websocketpp::connection_hdl hdl, message_ptr msg)
- {
- m_lbRecvMsg.InsertString(0,msg->get_payload().c_str());
- }
|