Skip to content

Commit

Permalink
Implement basic version of web socket server #19
Browse files Browse the repository at this point in the history
Create initial CMakeList

Implement basic version of chat server
  • Loading branch information
Vicx95 committed Mar 29, 2024
1 parent 994771c commit 2f3c600
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
25 changes: 25 additions & 0 deletions server/CMakeLists.txt
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}
)
56 changes: 56 additions & 0 deletions server/chatserver.cpp
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();
}
}
23 changes: 23 additions & 0 deletions server/chatserver.h
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;
};
10 changes: 10 additions & 0 deletions server/main.cpp
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();
}

0 comments on commit 2f3c600

Please sign in to comment.