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

时间:2016-07-31 14:49:35

标签: c++ qt

我有一个简单的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());
}

它工作正常,但如果用户写了一条消息,它就会出现在textedit上,并且每个连接的人都可以看到它。我想要做的是能够向我从用户listwidget中选择的特定用户发送私人消息,类似于Skype的说法。

例如,如果我是用户1而我从listwidget中选择用户2,我想向他发送一条用户3无法看到的消息。

对不起我的英语不好以及这个愚蠢的问题,但我无法弄清楚如何解决这个问题。我很乐意接受任何建议。

1 个答案:

答案 0 :(得分:1)

根据on_sayButton_clickedreadyRead的实施情况判断,您的协议仅支持广播消息,因为发送到服务器的所有内容都无条件地发送给所有当前连接的用户。

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

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