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.

125 lines
3.7KB

  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/WebSocketSession.h"
  16. using namespace std;
  17. using namespace toolkit;
  18. using namespace mediakit;
  19. /**
  20. * 回显会话
  21. */
  22. class EchoSession : public Session {
  23. public:
  24. EchoSession(const Socket::Ptr &pSock) : Session(pSock){
  25. DebugL;
  26. }
  27. virtual ~EchoSession(){
  28. DebugL;
  29. }
  30. void attachServer(const Server &server) override{
  31. DebugL << getIdentifier() << " " << Session::getIdentifier();
  32. }
  33. void onRecv(const Buffer::Ptr &buffer) override {
  34. //回显数据
  35. SockSender::send("from EchoSession:");
  36. send(buffer);
  37. }
  38. void onError(const SockException &err) override{
  39. WarnL << err.what();
  40. }
  41. //每隔一段时间触发,用来做超时管理
  42. void onManager() override{
  43. DebugL;
  44. }
  45. };
  46. class EchoSessionWithUrl : public Session {
  47. public:
  48. EchoSessionWithUrl(const Socket::Ptr &pSock) : Session(pSock){
  49. DebugL;
  50. }
  51. virtual ~EchoSessionWithUrl(){
  52. DebugL;
  53. }
  54. void attachServer(const Server &server) override{
  55. DebugL << getIdentifier() << " " << Session::getIdentifier();
  56. }
  57. void onRecv(const Buffer::Ptr &buffer) override {
  58. //回显数据
  59. SockSender::send("from EchoSessionWithUrl:");
  60. send(buffer);
  61. }
  62. void onError(const SockException &err) override{
  63. WarnL << err.what();
  64. }
  65. //每隔一段时间触发,用来做超时管理
  66. void onManager() override{
  67. DebugL;
  68. }
  69. };
  70. /**
  71. * 此对象可以根据websocket 客户端访问的url选择创建不同的对象
  72. */
  73. struct EchoSessionCreator {
  74. //返回的Session必须派生于SendInterceptor,可以返回null(拒绝连接)
  75. Session::Ptr operator()(const Parser &header, const HttpSession &parent, const Socket::Ptr &pSock) {
  76. // return nullptr;
  77. if (header.Url() == "/") {
  78. return std::make_shared<SessionTypeImp<EchoSession> >(header, parent, pSock);
  79. }
  80. return std::make_shared<SessionTypeImp<EchoSessionWithUrl> >(header, parent, pSock);
  81. }
  82. };
  83. int main(int argc, char *argv[]) {
  84. //设置日志
  85. Logger::Instance().add(std::make_shared<ConsoleChannel>());
  86. Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
  87. SSL_Initor::Instance().loadCertificate((exeDir() + "ssl.p12").data());
  88. {
  89. TcpServer::Ptr httpSrv(new TcpServer());
  90. //http服务器,支持websocket
  91. httpSrv->start<WebSocketSessionBase<EchoSessionCreator, HttpSession> >(80);//默认80
  92. TcpServer::Ptr httpsSrv(new TcpServer());
  93. //https服务器,支持websocket
  94. httpsSrv->start<WebSocketSessionBase<EchoSessionCreator, HttpsSession> >(443);//默认443
  95. TcpServer::Ptr httpSrvOld(new TcpServer());
  96. //兼容之前的代码(但是不支持根据url选择生成Session类型)
  97. httpSrvOld->start<WebSocketSession<EchoSession, HttpSession> >(8080);
  98. DebugL << "请打开网页:http://www.websocket-test.com/,进行测试";
  99. DebugL << "连接 ws://127.0.0.1/xxxx,ws://127.0.0.1/ 测试的效果将不同,支持根据url选择不同的处理逻辑";
  100. //设置退出信号处理函数
  101. static semaphore sem;
  102. signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
  103. sem.wait();
  104. }
  105. return 0;
  106. }