中间件底层,websocket

testClient.cpp 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // testClient.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <memory>
  6. #include <thread>
  7. #include <regex>
  8. #include "NetInterface.h"
  9. #include "PduEntity.h"
  10. int main()
  11. {
  12. auto pClient = CNetInterface::getNetInstance();
  13. pClient->setLinkStateChanged([=](const PduLinkContent& linkContent) {
  14. auto linkState = linkContent.nLinkState;
  15. if (PDU_LINK_STATE_SUCCESSED == linkState) {
  16. std::cout << "连接成功" << std::endl;
  17. }
  18. else if (PDU_LINK_STATE_FAILED == linkState) {
  19. std::cout << "连接失败" << std::endl;
  20. }
  21. else if (PDU_LINK_STATE_DISCONNECTED == linkState) {
  22. std::cout << "连接断开" << std::endl;
  23. }
  24. else if (PDU_LINK_STATE_REG_OK == linkState) {
  25. std::cout << "注册成功" << std::endl;
  26. }
  27. else if (PDU_LINK_STATE_REG_FAILED == linkState) {
  28. std::cout << "注册失败" << std::endl;
  29. }
  30. });
  31. pClient->setRecvCommand([=](CPduEntity* a_pPduEntity) {
  32. PDU_CMD_TYPE CmdType = a_pPduEntity->GetCmdType();
  33. if (PDU_CMD_LISTEN == CmdType) {
  34. std::cout << "接收心跳" << std::endl;
  35. }
  36. else if (PDU_CMD_DEV_ONLINE_LIST == CmdType) {
  37. std::cout << "在线设备列表"<< a_pPduEntity->GetDataString(0).GetBuffer(0) << std::endl;
  38. auto devList = a_pPduEntity->GetDataString(0);
  39. std::string str = devList.GetBuffer(0);
  40. devList.ReleaseBuffer();
  41. std::smatch match;
  42. auto citer = str.cbegin();
  43. while (std::regex_search(citer, str.cend(), match, std::regex(R"(\d{1,2}\|)"))) {
  44. std::cout << match[0] << std::endl;
  45. citer = match[0].second;
  46. std::cout << std::endl;
  47. }
  48. }
  49. });
  50. pClient->CreatePduClient("127.0.0.1", 8811, PDU_DEV_TYPE_ACD, 1,true);
  51. std::this_thread::sleep_for(std::chrono::seconds(20));
  52. //pClient->ClosePduClient(PDU_DEV_TYPE_CTI, 1);
  53. //std::this_thread::sleep_for(std::chrono::seconds(20));
  54. std::cout << "已全部关闭" << std::endl;
  55. for (;;) {}
  56. return 0;
  57. }