websocket.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Copyright (c) 2020-2021 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef RTC_WEBSOCKET_H
  19. #define RTC_WEBSOCKET_H
  20. #if RTC_ENABLE_WEBSOCKET
  21. #include "channel.hpp"
  22. #include "include.hpp"
  23. #include "init.hpp"
  24. #include "message.hpp"
  25. #include "queue.hpp"
  26. namespace rtc {
  27. namespace impl {
  28. struct WebSocket;
  29. }
  30. class RTC_CPP_EXPORT WebSocket final : private CheshireCat<impl::WebSocket>, public Channel {
  31. public:
  32. enum class State : int {
  33. Connecting = 0,
  34. Open = 1,
  35. Closing = 2,
  36. Closed = 3,
  37. };
  38. struct Configuration {
  39. bool disableTlsVerification = false; // if true, don't verify the TLS certificate
  40. std::vector<string> protocols;
  41. };
  42. WebSocket();
  43. WebSocket(Configuration config);
  44. ~WebSocket();
  45. State readyState() const;
  46. bool isOpen() const override;
  47. bool isClosed() const override;
  48. size_t maxMessageSize() const override;
  49. void open(const string &url);
  50. void close() override;
  51. bool send(const message_variant data) override;
  52. bool send(const byte *data, size_t size) override;
  53. private:
  54. using CheshireCat<impl::WebSocket>::impl;
  55. };
  56. } // namespace rtc
  57. #endif
  58. #endif // RTC_WEBSOCKET_H