2
0

websocket.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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"
  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. using Configuration = WebSocketConfiguration;
  27. WebSocket();
  28. WebSocket(Configuration config);
  29. WebSocket(impl_ptr<impl::WebSocket> impl);
  30. ~WebSocket() override;
  31. State readyState() const;
  32. bool isOpen() const override;
  33. bool isClosed() const override;
  34. size_t maxMessageSize() const override;
  35. void open(const string &url);
  36. void close() override;
  37. void forceClose();
  38. bool send(const message_variant data) override;
  39. bool send(const byte *data, size_t size) override;
  40. optional<string> remoteAddress() const;
  41. optional<string> path() const;
  42. private:
  43. using CheshireCat<impl::WebSocket>::impl;
  44. };
  45. std::ostream &operator<<(std::ostream &out, WebSocket::State state);
  46. } // namespace rtc
  47. #endif
  48. #endif // RTC_WEBSOCKET_H