Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

71 lines
2.6KB

  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <csignal>
  11. #include "Util/util.h"
  12. #include "Util/logger.h"
  13. #include "Util/NoticeCenter.h"
  14. using namespace std;
  15. using namespace toolkit;
  16. //广播名称1
  17. #define NOTICE_NAME1 "NOTICE_NAME1"
  18. //广播名称2
  19. #define NOTICE_NAME2 "NOTICE_NAME2"
  20. //程序退出标记
  21. bool g_bExitFlag = false;
  22. int main() {
  23. //设置程序退出信号处理函数
  24. signal(SIGINT, [](int){g_bExitFlag = true;});
  25. //设置日志
  26. Logger::Instance().add(std::make_shared<ConsoleChannel>());
  27. //对事件NOTICE_NAME1新增一个监听
  28. //addListener方法第一个参数是标签,用来删除监听时使用
  29. //需要注意的是监听回调的参数列表个数类型需要与emitEvent广播时的完全一致,否则会有无法预知的错误
  30. NoticeCenter::Instance().addListener(0,NOTICE_NAME1,
  31. [](int &a,const char * &b,double &c,string &d){
  32. DebugL << a << " " << b << " " << c << " " << d;
  33. NoticeCenter::Instance().delListener(0,NOTICE_NAME1);
  34. NoticeCenter::Instance().addListener(0,NOTICE_NAME1,
  35. [](int &a,const char * &b,double &c,string &d){
  36. InfoL << a << " " << b << " " << c << " " << d;
  37. });
  38. });
  39. //监听NOTICE_NAME2事件
  40. NoticeCenter::Instance().addListener(0,NOTICE_NAME2,
  41. [](string &d,double &c,const char *&b,int &a){
  42. DebugL << a << " " << b << " " << c << " " << d;
  43. NoticeCenter::Instance().delListener(0,NOTICE_NAME2);
  44. NoticeCenter::Instance().addListener(0,NOTICE_NAME2,
  45. [](string &d,double &c,const char *&b,int &a){
  46. WarnL << a << " " << b << " " << c << " " << d;
  47. });
  48. });
  49. int a = 0;
  50. while(!g_bExitFlag){
  51. const char *b = "b";
  52. double c = 3.14;
  53. string d("d");
  54. //每隔1秒广播一次事件,如果无法确定参数类型,可加强制转换
  55. NoticeCenter::Instance().emitEvent(NOTICE_NAME1,++a,(const char *)"b",c,d);
  56. NoticeCenter::Instance().emitEvent(NOTICE_NAME2,d,c,b,a);
  57. sleep(1); // sleep 1 second
  58. }
  59. return 0;
  60. }