websocket.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Copyright (c) 2020-2021 Paul-Louis Ageneau
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #ifndef RTC_WEBSOCKET_H
  9. #define RTC_WEBSOCKET_H
  10. #if RTC_ENABLE_WEBSOCKET
  11. #include "channel.hpp"
  12. #include "common.hpp"
  13. #include "configuration.hpp" // for ProxyServer
  14. namespace rtc {
  15. namespace impl {
  16. struct WebSocket;
  17. }
  18. class RTC_CPP_EXPORT WebSocket final : private CheshireCat<impl::WebSocket>, public Channel {
  19. public:
  20. enum class State : int {
  21. Connecting = 0,
  22. Open = 1,
  23. Closing = 2,
  24. Closed = 3,
  25. };
  26. struct Configuration {
  27. bool disableTlsVerification = false; // if true, don't verify the TLS certificate
  28. optional<ProxyServer> proxyServer; // only non-authenticated http supported for now
  29. std::vector<string> protocols;
  30. optional<std::chrono::milliseconds> connectionTimeout; // zero to disable
  31. optional<std::chrono::milliseconds> pingInterval; // zero to disable
  32. optional<int> maxOutstandingPings;
  33. };
  34. WebSocket();
  35. WebSocket(Configuration config);
  36. WebSocket(impl_ptr<impl::WebSocket> impl);
  37. ~WebSocket() override;
  38. State readyState() const;
  39. bool isOpen() const override;
  40. bool isClosed() const override;
  41. size_t maxMessageSize() const override;
  42. void open(const string &url);
  43. void close() override;
  44. void forceClose();
  45. bool send(const message_variant data) override;
  46. bool send(const byte *data, size_t size) override;
  47. optional<string> remoteAddress() const;
  48. optional<string> path() const;
  49. private:
  50. using CheshireCat<impl::WebSocket>::impl;
  51. };
  52. } // namespace rtc
  53. #endif
  54. #endif // RTC_WEBSOCKET_H