Able to send and receive messages, server panicking and not relaying userids yet

This commit is contained in:
2026-02-09 20:35:17 +01:00
parent a0d9d9c6de
commit 38f6488ba1
12 changed files with 171 additions and 13 deletions

View File

@@ -1,20 +1,45 @@
#include "ui/mainwindow.h"
#include "logic/client_reactor.h"
#include "messages.pb.h"
#include "ui_mainwindow.h"
#include <grpcpp/create_channel.h>
#include <grpcpp/security/credentials.h>
#include <memory>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
auto channel = grpc::CreateChannel("localhost:50051",
grpc::InsecureChannelCredentials());
auto stub = chat::Chat::NewStub(channel);
m_reactor = std::make_unique<Reactor>(stub.get());
connect(ui->sendButton, &QPushButton::clicked, this, [this]() {
auto msg = ui->inputText->toPlainText();
receiveMsg(msg);
if (!msg.isEmpty()) {
sendMsg(msg);
ui->inputText->clear();
}
});
connect(m_reactor->getHandler().get(), &MessageHandler::messageReceived, this,
&MainWindow::receiveMsg);
}
MainWindow::~MainWindow() { delete ui; }
void MainWindow::receiveMsg(QString &msg) {
Message message(0, QDateTime::currentDateTime(), msg);
void MainWindow::sendMsg(const QString &msg) {
if (m_reactor && m_reactor->IsConnected()) {
chat::chatMsg chatMsg;
chatMsg.set_message(msg.toStdString());
m_reactor->SendMessage(chatMsg);
}
}
void MainWindow::receiveMsg(const chat::chatMsg &chatMsg) {
auto userId = QString::fromStdString(chatMsg.userid());
auto content = QString::fromStdString(chatMsg.message());
Message message(userId, QDateTime::currentDateTime(), content);
m_chatroom.addMessage(message);
ui->outputText->setText(m_chatroom.getMessagesString());
}