-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement basic version of web socket server #19
Create initial CMakeList Implement basic version of chat server
- Loading branch information
Showing
4 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
cmake_minimum_required(VERSION 3.14) | ||
|
||
project(server LANGUAGES CXX) | ||
|
||
set(CMAKE_AUTOUIC ON) | ||
set(CMAKE_AUTOMOC ON) | ||
set(CMAKE_AUTORCC ON) | ||
|
||
set(CMAKE_CXX_STANDARD 20) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
||
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core WebSockets) | ||
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core WebSockets) | ||
|
||
add_executable(server | ||
main.cpp | ||
chatserver.h chatserver.cpp | ||
) | ||
target_link_libraries(server Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::WebSockets) | ||
|
||
include(GNUInstallDirs) | ||
install(TARGETS server | ||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} | ||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include "chatserver.h" | ||
|
||
static QString getIdentifier(QWebSocket* socket) | ||
{ | ||
return QStringLiteral("%1:%2").arg(socket->peerAddress().toString(), | ||
QString::number(socket->peerPort())); | ||
} | ||
|
||
ChatServer::ChatServer(quint16 port, QObject *parent) | ||
: QObject{parent}, | ||
m_webSocketServer{new QWebSocketServer("Chat Server", | ||
QWebSocketServer::NonSecureMode, | ||
this)} | ||
{ | ||
if(m_webSocketServer->listen(QHostAddress::Any, port)) | ||
{ | ||
qInfo() << "Server listening on port " << port; | ||
connect(&*m_webSocketServer, &QWebSocketServer::newConnection, this, &ChatServer::onNewConnection); | ||
} | ||
} | ||
|
||
ChatServer::~ChatServer(){ | ||
m_webSocketServer->close(); | ||
} | ||
|
||
void ChatServer::onNewConnection() | ||
{ | ||
auto clientSocket = m_webSocketServer->nextPendingConnection(); | ||
qInfo() << getIdentifier(clientSocket) << " connected!"; | ||
clientSocket->setParent(this); | ||
|
||
connect(clientSocket, &QWebSocket::textMessageReceived, this, &ChatServer::processMessage); | ||
connect(clientSocket, &QWebSocket::disconnected, this, &ChatServer::socketDisconnected); | ||
|
||
m_clients << clientSocket; | ||
} | ||
|
||
void ChatServer::processMessage(const QString& message) | ||
{ | ||
QWebSocket* clientSender = qobject_cast<QWebSocket *>(sender()); | ||
for (QWebSocket* client : std::as_const(m_clients)) { | ||
if (client != clientSender) | ||
client->sendTextMessage(message); | ||
} | ||
} | ||
|
||
void ChatServer::socketDisconnected() | ||
{ | ||
QWebSocket* client = qobject_cast<QWebSocket*>(sender()); | ||
qInfo() << getIdentifier(client) << " disconnected!"; | ||
if(client) | ||
{ | ||
m_clients.removeAll(client); | ||
client->deleteLater(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
#include <QObject> | ||
#include <QWebSocketServer> | ||
#include <QtWebSockets> | ||
#include <QDebug> | ||
|
||
class ChatServer : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit ChatServer(quint16 port, QObject *parent = nullptr); | ||
~ChatServer() override; | ||
|
||
private slots: | ||
void onNewConnection(); | ||
void processMessage(const QString& message); | ||
void socketDisconnected(); | ||
|
||
private: | ||
QWebSocketServer* m_webSocketServer; | ||
QList<QWebSocket*> m_clients; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include <QCoreApplication> | ||
#include "chatserver.h" | ||
|
||
int main(int argc, char *argv[]) { | ||
QCoreApplication a(argc, argv); | ||
|
||
ChatServer server(2000); | ||
|
||
return a.exec(); | ||
} |