chatserver.cpp Example File
simplechat/chatserver.cpp
#include "chatserver.h"
#include <QtWebSockets>
#include <QtCore>
#include <cstdio>
using namespace std;
QT_USE_NAMESPACE
static QString getIdentifier(QWebSocket *peer)
{
return QStringLiteral("%1:%2").arg(peer->peerAddress().toString(),
QString::number(peer->peerPort()));
}
ChatServer::ChatServer(quint16 port, QObject *parent) :
QObject(parent),
m_pWebSocketServer(new QWebSocketServer(QStringLiteral("Chat Server"),
QWebSocketServer::NonSecureMode,
this))
{
if (m_pWebSocketServer->listen(QHostAddress::Any, port))
{
QTextStream(stdout) << "Chat Server listening on port " << port << '\n';
connect(m_pWebSocketServer, &QWebSocketServer::newConnection,
this, &ChatServer::onNewConnection);
}
}
ChatServer::~ChatServer()
{
m_pWebSocketServer->close();
}
void ChatServer::onNewConnection()
{
auto pSocket = m_pWebSocketServer->nextPendingConnection();
QTextStream(stdout) << getIdentifier(pSocket) << " connected!\n";
pSocket->setParent(this);
connect(pSocket, &QWebSocket::textMessageReceived,
this, &ChatServer::processMessage);
connect(pSocket, &QWebSocket::disconnected,
this, &ChatServer::socketDisconnected);
m_clients << pSocket;
}
void ChatServer::processMessage(const QString &message)
{
QWebSocket *pSender = qobject_cast<QWebSocket *>(sender());
for (QWebSocket *pClient : qAsConst(m_clients)) {
if (pClient != pSender)
pClient->sendTextMessage(message);
}
}
void ChatServer::socketDisconnected()
{
QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
QTextStream(stdout) << getIdentifier(pClient) << " disconnected!\n";
if (pClient)
{
m_clients.removeAll(pClient);
pClient->deleteLater();
}
}