46 lines
1.4 KiB
C++
46 lines
1.4 KiB
C++
#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();
|
|
if (!msg.isEmpty()) {
|
|
sendMsg(msg);
|
|
ui->inputText->clear();
|
|
}
|
|
});
|
|
connect(m_reactor->getHandler().get(), &MessageHandler::messageReceived, this,
|
|
&MainWindow::receiveMsg);
|
|
}
|
|
|
|
MainWindow::~MainWindow() { delete ui; }
|
|
|
|
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());
|
|
}
|