Skip to content
Repos

DevPlaceApp

C++ 833 B36 lines Opens in site

Directory Raw Download

#pragma once
// Spec: SPEC.md FR-NET-05 (websocket). One client websocket with bounded reconnect. UI-thread API.
#include <functional>
#include <memory>
#include <string>
#include <vector>

#include "net/HttpTypes.hpp"

namespace dp {

class SocketSession {
 public:
  enum class State { Connecting, Open, Closed };

  struct Handlers {
    std::function<void()> onOpen;
    std::function<void(const std::string& text)> onText;
    std::function<void(const std::string& reason)> onClose;  // final close (no more reconnects)
  };

  SocketSession(std::string url, std::vector<std::pair<std::string, std::string>> headers, Handlers handlers);
  ~SocketSession();

  void open();
  void sendText(std::string text);
  void close();
  State state() const;

  struct Impl;

 private:
  std::shared_ptr<Impl> impl_;
};

}  // namespace dp
26.4ms