WebsocketClient.h 1.77 KB
#include <map>
#include <mutex>

#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>

#include "Message/CatalogParser.h"

#include "../rtp2/RTPReceiver2.h"

typedef websocketpp::client<websocketpp::config::asio_client> client;
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;

class WebsocketClient
{
public:
    static WebsocketClient* getInstance(){
		static WebsocketClient* singleton = nullptr;
		if (singleton == nullptr){
			singleton = new WebsocketClient();
		}
		return singleton;
	}

public:
    int init();

    void close();

    void terminate();

    int GetMinRtpPort();
    int GetMaxRtpPort();

    int InviteUdp(std::string sip_channel_id, int rtp_port, RTPReceiver2* r);
    int InviteTcp(std::string sip_channel_id, int rtp_port, RTPReceiver2* r);

    int ByeInvite(std::string sip_channel_id, int rtp_port);

    int DeleteReceiverPair(std::string sip_channel_id, int rtp_port);

public:
    void on_open(websocketpp::connection_hdl hdl);
    void on_close(websocketpp::connection_hdl hdl);

    void on_message(websocketpp::connection_hdl hdl, message_ptr msg);

    void on_fail(websocketpp::connection_hdl hdl);

private:
    bool readCfg();
    int connect();
    int reconnect();
    int check_connect();
    void send_text(std::string msg);

    int msg_parser(websocketpp::connection_hdl hdl, string msg);

    void cache_receiver(std::string sip_channel_id, int rtp_port, RTPReceiver2* r);

    void response_invite_failed(std::string rKey);

private:
  client m_client;
  websocketpp::lib::shared_ptr<websocketpp::lib::thread> thread_;
  websocketpp::connection_hdl m_hdl;
  std::string uri;

  ServerInfo mInfo;

  bool mbClosed{false};

  std::map<string, RTPReceiver2*> m_receiver_map;
  std::mutex m_receiver_map_mtx;
};