websocket.hpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. } // namespace rtc
  46. #endif
  47. #endif // RTC_WEBSOCKET_H