-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChannels.cpp
More file actions
41 lines (34 loc) · 965 Bytes
/
Channels.cpp
File metadata and controls
41 lines (34 loc) · 965 Bytes
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
#include "channels.h"
#include <QDebug>
channels::channels(QTcpSocket* socket, QObject *parent)
: QObject(parent), socket(socket)
{
connect(socket, &QTcpSocket::readyRead, this, &channels::onReadyRead);
connect(socket, &QTcpSocket::disconnected, this, &channels::onDisconnected);
}
channels::~channels()
{
if (socket) {
socket->disconnectFromHost();
socket->deleteLater();
}
}
void channels::onReadyRead()
{
QByteArray data = socket->readAll();
QString message = QString::fromUtf8(data);
qDebug() << "Message received from client:" << message;
emit messageReceived(message);
// Optional: Echo back to client
socket->write(QString("Echo: " + message).toUtf8());
}
void channels::onDisconnected()
{
qDebug() << "Client disconnected.";
emit clientDisconnected();
}
void channels::sendMessage(const QByteArray &data) {
if (socket && socket->isOpen()) {
socket->write(data);
}
}