You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.9KB

  1. /*
  2. * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
  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 <signal.h>
  11. #include <string>
  12. #include <iostream>
  13. #include "Util/MD5.h"
  14. #include "Util/logger.h"
  15. #include "Http/WebSocketClient.h"
  16. using namespace std;
  17. using namespace toolkit;
  18. using namespace mediakit;
  19. class EchoTcpClient : public TcpClient {
  20. public:
  21. EchoTcpClient(const EventPoller::Ptr &poller = nullptr){
  22. InfoL;
  23. }
  24. ~EchoTcpClient() override {
  25. InfoL;
  26. }
  27. protected:
  28. void onRecv(const Buffer::Ptr &pBuf) override {
  29. DebugL << pBuf->toString();
  30. }
  31. //被动断开连接回调
  32. void onErr(const SockException &ex) override {
  33. WarnL << ex.what();
  34. }
  35. //tcp连接成功后每2秒触发一次该事件
  36. void onManager() override {
  37. SockSender::send("echo test!");
  38. DebugL << "send echo test";
  39. }
  40. //连接服务器结果回调
  41. void onConnect(const SockException &ex) override{
  42. DebugL << ex.what();
  43. }
  44. //数据全部发送完毕后回调
  45. void onFlush() override{
  46. DebugL;
  47. }
  48. };
  49. int main(int argc, char *argv[]) {
  50. //设置退出信号处理函数
  51. static semaphore sem;
  52. signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
  53. //设置日志
  54. Logger::Instance().add(std::make_shared<ConsoleChannel>());
  55. Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
  56. {
  57. WebSocketClient<EchoTcpClient>::Ptr client = std::make_shared<WebSocketClient<EchoTcpClient> >();
  58. client->startConnect("127.0.0.1", 80);
  59. sem.wait();
  60. }
  61. return 0;
  62. }