-
Notifications
You must be signed in to change notification settings - Fork 4
/
livedanmuhandler.cpp
144 lines (131 loc) · 5.23 KB
/
livedanmuhandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "livedanmuhandler.h"
LiveDanmuHandler::LiveDanmuHandler(QObject *parent) : QObject(parent)
{
serverClient = NULL;
}
void LiveDanmuHandler::connectServer(int roomID)
{
serverClient = new ServerClient();
connect(serverClient, SIGNAL(actionPrepared(Intent)), this, SLOT(dispatchAction(Intent)));
connect(serverClient, SIGNAL(dataReceived(QByteArray)), this, SLOT(parseDataToDanmu(QByteArray)));
serverClient->doConnect(roomID);
}
void LiveDanmuHandler::disConnServer()
{
if (serverClient != NULL)
{
serverClient->doDisConn();
delete serverClient;
serverClient = NULL;
}
}
void LiveDanmuHandler::dispatchAction(Intent intent)
{
emit actionPrepared(intent);
}
void LiveDanmuHandler::parseDataToDanmu(QByteArray data)
{
if (!data.isNull() && !data.isEmpty())
{
QDataStream dataStream(&data, QIODevice::ReadOnly);
qint64 dataLength = data.length();
if (dataLength < 16)
{
emit actionPrepared(Intent(Intent::ACTION_LOG_ERROR,"接收到的数据异常"));
}
else if (dataLength > 16)
{
int givenLength;
dataStream >> givenLength;
if (dataLength == givenLength)
{
short param1, param2;
int action, param4;
dataStream >> param1 >> param2 >> action >> param4;
action -= 1;
// qDebug() << action;
if (action == 2)
{
int userCount;
dataStream >> userCount;
// qDebug() << userCount;
emit viewerCountChanged(userCount);
}
else if (action == 4)
{
uint len = givenLength - 16;
char *msgJsonBytes = new char[len + 1]();
dataStream.readRawData(msgJsonBytes, len);
QString msgJsonStr = QString::fromUtf8(msgJsonBytes);
delete []msgJsonBytes;
// qDebug() << len;
// qDebug() << "json: " << msgJsonStr;
QJsonDocument msgDoc = QJsonDocument::fromJson(msgJsonStr.toUtf8());
if (!msgDoc.isNull())
{
QJsonObject msgObj = msgDoc.object();
QString msgType = msgObj["cmd"].toString();
// 弹幕封装
BiliLiveDanmuToolPlugin::ReceivedDanmukuProperties danmukuPros;
danmukuPros.rawData = msgJsonStr.toUtf8();
danmukuPros.danmukuTypeStr = msgType;
if (msgType == "DANMU_MSG")
{
// int uid = msgObj["info"][2][0];
QJsonArray info = msgObj["info"].toArray();
QString uname = info[2].toArray()[1].toString();
QString danmuku = info[1].toString();
danmukuPros.danmukuType = danmukuPros.DANMUKU;
danmukuPros.userName = uname;
danmukuPros.danmukuText = danmuku;
}
else if(msgType == "SEND_GIFT")
{
QJsonObject data = msgObj["data"].toObject();
QString giftName = data["giftName"].toString();
QString uname = data["uname"].toString();
int giftNum = data["num"].toInt();
danmukuPros.danmukuType = danmukuPros.GIFT;
danmukuPros.userName = uname;
danmukuPros.giftName = giftName;
danmukuPros.giftNum = giftNum;
}
else if(msgType == "WELCOME")
{
QJsonObject data = msgObj["data"].toObject();
QString uname = data["uname"].toString();
danmukuPros.danmukuType = danmukuPros.WELCOME;
danmukuPros.userName = uname;
}
else
{
danmukuPros.danmukuType = -999;
}
emit messageReceived(danmukuPros);
}
else
{
qDebug() << "无法生成正确的json";
}
}
}
else if (16 < givenLength && givenLength < dataLength)
{
// qDebug() << "多条信息" << givenLength << dataLength;
QByteArray firstData = data.mid(0, givenLength);
QByteArray remainData = data.mid(givenLength);
parseDataToDanmu(firstData);
parseDataToDanmu(remainData);
}
}
else
{
int givenLength;
dataStream >> givenLength;
if (givenLength == 16)
{
emit actionPrepared(Intent(Intent::ACTION_LOG_INFO,"服务器认证成功"));
}
}
}
}