gpt4 book ai didi

c++ - Qt TCP 客户端/服务器聊天应用程序。如何发送私信

转载 作者:行者123 更新时间:2023-11-28 05:34:17 25 4
gpt4 key购买 nike

我有一个简单的 tcp 客户端/服务器聊天应用程序,如下所示: enter image description here这是我的客户端源代码:

#include "MainWindow.h"

// We'll need some regular expression magic in this code:
#include <QRegExp>

// This is our MainWindow constructor (you C++ n00b)
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
// When using Designer, you should always call setupUi(this)
// in your constructor. This creates and lays out all the widgets
// on the MainWindow that you setup in Designer.
setupUi(this);

// Make sure that we are showing the login page when we startup:
stackedWidget->setCurrentWidget(loginPage);

// Instantiate our socket (but don't actually connect to anything
// yet until the user clicks the loginButton:
socket = new QTcpSocket(this);

// This is how we tell Qt to call our readyRead() and connected()
// functions when the socket has text ready to be read, and is done
// connecting to the server (respectively):
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
}

// This gets called when the loginButton gets clicked:
// We didn't have to use connect() to set this up because
// Qt recognizes the name of this function and knows to set
// up the signal/slot connection for us.
void MainWindow::on_loginButton_clicked()
{
// Start connecting to the chat server (on port 4200).
// This returns immediately and then works on connecting
// to the server in the background. When it's done, we'll
// get a connected() function call (below). If it fails,
// we won't get any error message because we didn't connect()
// to the error() signal from this socket.
socket->connectToHost(serverLineEdit->text(), 4200);
}

// This gets called when the user clicks the sayButton (next to where
// they type text to send to the chat room):
void MainWindow::on_sayButton_clicked()
{
// What did they want to say (minus white space around the string):
QString message = sayLineEdit->text().trimmed();

// Only send the text to the chat server if it's not empty:
if(!message.isEmpty())
{
socket->write(QString(message + "\n").toUtf8());
}

// Clear out the input box so they can type something else:
sayLineEdit->clear();

// Put the focus back into the input box so they can type again:
sayLineEdit->setFocus();
}

// This function gets called whenever the chat server has sent us some text:
void MainWindow::readyRead()
{
// We'll loop over every (complete) line of text that the server has sent us:
while(socket->canReadLine())
{
// Here's the line the of text the server sent us (we use UTF-8 so
// that non-English speakers can chat in their native language)
QString line = QString::fromUtf8(socket->readLine()).trimmed();

// These two regular expressions describe the kinds of messages
// the server can send us:

// Normal messges look like this: "username:The message"
QRegExp messageRegex("^([^:]+):(.*)$");

// Any message that starts with "/users:" is the server sending us a
// list of users so we can show that list in our GUI:
QRegExp usersRegex("^/users:(.*)$");

// Is this a users message:
if(usersRegex.indexIn(line) != -1)
{
// If so, udpate our users list on the right:
QStringList users = usersRegex.cap(1).split(",");
userListWidget->clear();
foreach(QString user, users)
new QListWidgetItem(QPixmap(":/user.png"), user, userListWidget);
}
// Is this a normal chat message:
else if(messageRegex.indexIn(line) != -1)
{
// If so, append this message to our chat box:
QString user = messageRegex.cap(1);
QString message = messageRegex.cap(2);

roomTextEdit->append("<b>" + user + "</b>: " + message);
}
}
}
void MainWindow::onListWidgetItemClicked(const QModelIndex &index)
{

}

// This function gets called when our socket has successfully connected to the chat
// server. (see the connect() call in the MainWindow constructor).
void MainWindow::connected()
{
// Flip over to the chat page:
stackedWidget->setCurrentWidget(chatPage);

// And send our username to the chat server.
socket->write(QString("/me:" + userLineEdit->text() + "\n").toUtf8());
}

它工作正常,但如果一个用户写了一条消息,它会出现在文本编辑器上,并且连接的每个人都可以看到它。我想要做的是能够向我从用户列表小部件中选择的特定用户发送私有(private)消息,类似于 Skype 的东西。

例如,如果我是用户 1,我从列表小部件中选择了用户 2,我想向他发送一条用户 3 无法看到的消息。

对于我糟糕的英语和这个愚蠢的问题,我深表歉意,但我不知道如何解决这个问题。我很乐意接受任何建议。

最佳答案

on_sayButton_clickedreadyRead 的实现来看,您的协议(protocol)仅支持广播消息,因为发送到服务器的所有内容都会无条件地发送给所有当前连接的用户。

您必须在协议(protocol)中引入单独的消息类型,以指示服务器仅将消息发送给给定的用户。看起来您是通过测试字符串开头的特定标记来区分普通消息和控制消息。如果您想更进一步,您可以将 private:username:message 指定为应该只发送给 username 的数据包的开头。然后,服务器可以使用 username 查找用户的 IP,并仅将 message 发送到它的套接字,可能还有另一个额外的 token 来识别这是一条私有(private)消息,而不是应该显示在一般聊天窗口中的。

请记住,您当前的实现允许用户通过在输入框中输入适当的字符串来简单地发送服务器消息。我建议创建一个完全独立的类,它接受表示输入消息的对象,并通过套接字将它们发送到服务器。类似地,它为 signal 提供了一个对象,该对象表示将消息发送给用户时的消息。这样,您就从 GUI 逻辑中抽象出了服务器消息的序列化和反序列化,并且您可以轻松地更改客户端-服务器通信代码的实现,而无需重新执行 GUI。如果您决定这样做,您应该在服务器和客户端之间重复使用相同的代码,如果可能的话:这将为您省去许多可能因服务器和客户端使用不同的代码生成(或提取)实际代码而引起的麻烦来自接收到的数据包的消息。

关于c++ - Qt TCP 客户端/服务器聊天应用程序。如何发送私信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38685154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com