3#include "IceSignalWebSocket.h"
5#include <nlohmann/json.hpp>
11 m_webSocket = std::make_shared<rtc::WebSocket>();
14CIceSignalWebSocket::~CIceSignalWebSocket()
16 qDebug() <<
"CIceSignalWebSocket::~CIceSignalWebSocket()";
19int CIceSignalWebSocket::Write(
const char *buf,
int nLen)
22 QMutexLocker lock(&m_MutexSend);
25 bRet = m_webSocket->send(std::string(buf, nLen));
32int CIceSignalWebSocket::Read(
char *buf,
int nLen)
39int CIceSignalWebSocket::Open(
const std::string &szServer, quint16 nPort,
40 const std::string &user,
const std::string &password)
44 std::string wsPrefix =
"ws://";
46 const std::string url = wsPrefix + szServer +
":" +
47 std::to_string(nPort) +
"/" + user;
48 qDebug(m_Log,
"Url is %s", url.c_str());
49 return Open(url, user, password);
52int CIceSignalWebSocket::Open(
const std::string &szUrl,
53 const std::string &user,
54 const std::string &password)
58 m_webSocket = std::make_shared<rtc::WebSocket>();
59 if(!m_webSocket)
return -1;
61 m_webSocket->onOpen([
this]() {
62 qDebug(m_Log,
"WebSocket is open");
65 m_webSocket->onError([
this](std::string szErr) {
66 qDebug(m_Log,
"WebSocket is error");
67 emit sigError(-1, szErr.c_str());
69 m_webSocket->onClosed([
this]() {
70 qDebug(m_Log,
"WebSocket is close");
71 emit sigDisconnected();
73 m_webSocket->onMessage([
this](std::variant<rtc::binary, std::string> data) {
74 if (!std::holds_alternative<std::string>(data))
77 nlohmann::json message = nlohmann::json::parse(std::get<std::string>(data));
79 auto it = message.find(
"id");
80 if (it == message.end())
82 it = message.find(
"toUser");
83 if (it == message.end())
85 std::string toUser = it->get<std::string>();
86 it = message.find(
"fromUser");
87 if (it == message.end())
89 std::string fromUser = it->get<std::string>();
90 it = message.find(
"channelid");
91 if (it == message.end())
93 std::string channelid = it->get<std::string>();
95 it = message.find(
"type");
96 if (it == message.end())
98 std::string type = it->get<std::string>();
102 auto sdp = message[
"description"].get<std::string>();
103 emit
sigOffer(fromUser.c_str(), toUser.c_str(), channelid.c_str(),
104 type.c_str(), sdp.c_str());
106 else if(type ==
"answer") {
107 auto sdp = message[
"description"].get<std::string>();
109 channelid.c_str(), type.c_str(), sdp.c_str());
110 }
else if (type ==
"candidate") {
111 auto sdp = message[
"candidate"].get<std::string>();
112 auto mid = message[
"mid"].get<std::string>();
113 emit sigCandiate(fromUser.c_str(), toUser.c_str(),
114 channelid.c_str(), mid.c_str(), sdp.c_str());
121 m_webSocket->open(szUrl);
122 }
catch (std::exception &e) {
124 "Open websocket fail: %s; %s",
125 e.what(), szUrl.c_str());
132int CIceSignalWebSocket::Close()
134 if(!m_webSocket)
return -1;
135 m_webSocket->close();
139bool CIceSignalWebSocket::IsOpen()
142 return m_webSocket->isOpen();
146int CIceSignalWebSocket::SendCandiate(
const QString& toUser,
147 const QString &channelId,
148 const rtc::Candidate &candidate,
149 const QString &fromUser)
151 std::string user = fromUser.toStdString();
154 nlohmann::json message = {{
"id", toUser.toStdString()},
155 {
"toUser", toUser.toStdString()},
157 {
"channelid", channelId.toStdString()},
158 {
"type",
"candidate"},
159 {
"candidate", std::string(candidate)},
160 {
"mid", candidate.mid()}};
162 std::string m = message.dump();
163 return Write(m.c_str(), m.size());
166int CIceSignalWebSocket::SendDescription(
const QString& toUser,
167 const QString& channelId,
168 const rtc::Description &description,
169 const QString& fromUser)
171 std::string user = fromUser.toStdString();
174 nlohmann::json message = {
175 {
"id", toUser.toStdString()},
176 {
"toUser", toUser.toStdString()},
178 {
"channelid", channelId.toStdString()},
179 {
"type", description.typeString()},
180 {
"description", std::string(description)}};
182 std::string m = message.dump();
183 return Write(m.c_str(), m.size());
CIceSignalWebSocket(QObject *parent=nullptr)
The ICE signal interface class.
void sigOffer(const QString &fromUser, const QString &toUser, const QString &channelId, const QString &type, const QString &sdp)
sigOffer
void sigDescription(const QString &fromUser, const QString &toUser, const QString &channelId, const QString &type, const QString &sdp)
sigDescription