починил нейроконструктор, исправил распознавание сообщений из чата
This commit is contained in:
+62
-29
@@ -5,6 +5,8 @@
|
||||
#include <QDateTime>
|
||||
#include <QCoreApplication>
|
||||
#include <synchapi.h>
|
||||
#include "logmanager.h"
|
||||
#include "qregularexpression.h"
|
||||
|
||||
WebSocketClient::WebSocketClient(QObject *parent) :
|
||||
QObject(parent),
|
||||
@@ -24,6 +26,7 @@ WebSocketClient::~WebSocketClient()
|
||||
delete m_webSocket;
|
||||
}
|
||||
|
||||
|
||||
bool WebSocketClient::connectToServer(const QString &url)
|
||||
{
|
||||
if (m_webSocket) {
|
||||
@@ -154,42 +157,72 @@ void WebSocketClient::onPingTimeout()
|
||||
|
||||
void WebSocketClient::onTextMessageReceived(const QString &message)
|
||||
{
|
||||
QStringList lines = message.split("\r\n", Qt::SkipEmptyParts);
|
||||
|
||||
// Обработка PING от сервера
|
||||
if (message.startsWith("PING")) {
|
||||
send("PONG :tmi.twitch.tv\r\n");
|
||||
return;
|
||||
}
|
||||
for (const QString &line : lines) {
|
||||
if (line.isEmpty()) continue;
|
||||
|
||||
// Обработка успешной аутентификации
|
||||
if (message.contains("001") && !m_currentChannel.isEmpty()) {
|
||||
joinChannel(m_currentChannel);
|
||||
return; // Добавить return после первого успешного подключения
|
||||
}
|
||||
|
||||
// Обработка сообщений чата
|
||||
if (message.contains("PRIVMSG")) {
|
||||
// Извлекаем информацию из сообщения
|
||||
// Формат: :nick!nick@nick.tmi.twitch.tv PRIVMSG #channel :message
|
||||
QString cleanMessage = message;
|
||||
emit onNewMessage(cleanMessage);
|
||||
// Удаляем служебную информацию для чистого отображения
|
||||
int msgIndex = cleanMessage.indexOf("PRIVMSG");
|
||||
if (msgIndex != -1) {
|
||||
int channelIndex = cleanMessage.indexOf("#", msgIndex);
|
||||
int messageIndex = cleanMessage.indexOf(" :", channelIndex);
|
||||
if (messageIndex != -1) {
|
||||
QString chatMessage = cleanMessage.mid(messageIndex + 2).trimmed();
|
||||
QString sender = cleanMessage.mid(1, cleanMessage.indexOf("!") - 1);
|
||||
QString channel = cleanMessage.mid(channelIndex + 1,
|
||||
messageIndex - channelIndex - 1);
|
||||
qDebug() << "RAW LINE:" << line;
|
||||
|
||||
// Определяем тип сообщения
|
||||
bool isPing = line.startsWith("PING");
|
||||
bool isPrivmsg = false;
|
||||
bool isUserState = line.contains("USERSTATE");
|
||||
bool isRoomState = line.contains("ROOMSTATE");
|
||||
bool isUserNotice = line.contains("USERNOTICE");
|
||||
bool isJoin = line.contains(" JOIN ");
|
||||
bool isNamesList = (line.contains("353") || line.contains("366"));
|
||||
bool isCapAck = line.contains("CAP * ACK");
|
||||
|
||||
// Проверяем PRIVMSG отдельно, чтобы избежать ложных срабатываний
|
||||
if (line.contains("PRIVMSG", Qt::CaseInsensitive)) {
|
||||
// Проверяем структуру PRIVMSG сообщения
|
||||
// Должно быть: @теги :префикс PRIVMSG #канал :сообщение
|
||||
if (!isUserState && !isRoomState && !isUserNotice) {
|
||||
// Проверяем наличие канала и текста сообщения
|
||||
if (line.contains(" #") && line.contains(" :")) {
|
||||
isPrivmsg = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Обработка в зависимости от типа
|
||||
if (isPing) {
|
||||
send("PONG :tmi.twitch.tv\r\n");
|
||||
}
|
||||
else if (isCapAck) {
|
||||
qDebug() << "Capabilities acknowledged";
|
||||
}
|
||||
else if (isPrivmsg) {
|
||||
qDebug() << "PRIVMSG detected (final):" << line;
|
||||
emit onNewMessage(line);
|
||||
}
|
||||
else if (isJoin) {
|
||||
qDebug() << "JOIN detected:" << line;
|
||||
// emit onUserJoined(line);
|
||||
}
|
||||
else if (isUserState) {
|
||||
qDebug() << "USERSTATE detected:" << line;
|
||||
// emit onUserState(line);
|
||||
}
|
||||
else if (isRoomState) {
|
||||
qDebug() << "ROOMSTATE detected:" << line;
|
||||
// emit onRoomState(line);
|
||||
}
|
||||
else if (isUserNotice) {
|
||||
qDebug() << "USERNOTICE detected:" << line;
|
||||
// emit onUserNotice(line);
|
||||
}
|
||||
else if (isNamesList) {
|
||||
qDebug() << "NAMES list:" << line;
|
||||
}
|
||||
else if (line.contains("001") && !m_currentChannel.isEmpty()) {
|
||||
joinChannel(m_currentChannel);
|
||||
}
|
||||
else {
|
||||
qDebug() << "OTHER message:" << line;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void WebSocketClient::onErrorInternal(QAbstractSocket::SocketError error)
|
||||
|
||||
Reference in New Issue
Block a user