websocket.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. optional<string> caCertificatePemFile;
  34. };
  35. WebSocket();
  36. WebSocket(Configuration config);
  37. WebSocket(impl_ptr<impl::WebSocket> impl);
  38. ~WebSocket() override;
  39. State readyState() const;
  40. bool isOpen() const override;
  41. bool isClosed() const override;
  42. size_t maxMessageSize() const override;
  43. void open(const string &url);
  44. void close() override;
  45. void forceClose();
  46. bool send(const message_variant data) override;
  47. bool send(const byte *data, size_t size) override;
  48. optional<string> remoteAddress() const;
  49. optional<string> path() const;
  50. private:
  51. using CheshireCat<impl::WebSocket>::impl;
  52. };
  53. } // namespace rtc
  54. #endif
  55. #endif // RTC_WEBSOCKET_H