- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个简单的 tcp 客户端/服务器聊天应用程序,如下所示: 这是我的客户端源代码:
#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_clicked
和 readyRead
的实现来看,您的协议(protocol)仅支持广播消息,因为发送到服务器的所有内容都会无条件地发送给所有当前连接的用户。
您必须在协议(protocol)中引入单独的消息类型,以指示服务器仅将消息发送给给定的用户。看起来您是通过测试字符串开头的特定标记来区分普通消息和控制消息。如果您想更进一步,您可以将 private:username:message
指定为应该只发送给 username
的数据包的开头。然后,服务器可以使用 username
查找用户的 IP,并仅将 message
发送到它的套接字,可能还有另一个额外的 token 来识别这是一条私有(private)消息,而不是应该显示在一般聊天窗口中的。
请记住,您当前的实现允许用户通过在输入框中输入适当的字符串来简单地发送服务器消息。我建议创建一个完全独立的类,它接受表示输入消息的对象,并通过套接字将它们发送到服务器。类似地,它为 signal
提供了一个对象,该对象表示将消息发送给用户时的消息。这样,您就从 GUI 逻辑中抽象出了服务器消息的序列化和反序列化,并且您可以轻松地更改客户端-服务器通信代码的实现,而无需重新执行 GUI。如果您决定这样做,您应该在服务器和客户端之间重复使用相同的代码,如果可能的话:这将为您省去许多可能因服务器和客户端使用不同的代码生成(或提取)实际代码而引起的麻烦来自接收到的数据包的消息。
关于c++ - Qt TCP 客户端/服务器聊天应用程序。如何发送私信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38685154/
当用户执行命令时,我只想将输出发送回该用户,而不是 channel 。 我正在使用 PircBotX 框架。 我的代码: public void onMessage(MessageEvent even
我有一个名为“rbot”的自定义 Slack 机器人。我还创建了一个自定义斜杠命令“/r-out”。每当从任何 channel 执行此命令时,都会使用包含“channel_id”和“channel_n
我打算为我的用户制作一个 PM 系统,总的来说这看起来很容易,但是我看到制作 PM 系统的教程的方式有一个问题。 在我计划的工作方式中,会有像 user_from、user_to 这样的行,然后是消息
我正在尝试在我的 Titanium 应用程序中发送 Facebook Private。我尝试了以下两种方式,但没有取得任何成功。 方法一: var data = { link: "https:
我一直在网上搜索,但没有运气。我试图弄清楚如何将私有(private)消息从一个用户发送给另一个用户。有很多片段,但我不确定客户端/服务器交互。如果我有要发送到的套接字的 ID,我如何将其发送到服务器
我正在使用 phonegap 做一个简单的 android 项目。我必须从我的应用程序向 facebook 中的 friend 发送私有(private)消息。有什么方法可以通过 Url 发送消息吗?
我是一个nodejs菜鸟,但尝试与一些用户创建一个聊天室,它有效,但现在我想在聊天中使用命令“/w”发送私有(private)消息,例如: /w 大卫你好,你好吗? 只有 David 可以读取该消息。
如果有人可以帮助我,我有一个问题: 我希望我的应用程序使用 GraphAPI 接收 Instagram Direct 消息的 webhook 通知。 我尝试使用“instagram_manage_me
我需要使用 SLrequest 访问直接消息。我使用 twitter 的 Reverse oAuth 获得了 oAuthToken 和 oAuthToken Secret。现在我需要知道如何从 htt
有没有人知道如何通过 Slack RTM 向用户发送私有(private)消息?我正在尝试创建游戏,并想在 Slack 中私信一位用户以提供更新等。 你可以通过 https://github.com/
我希望机器人向某人发送直接消息,但我不知道该怎么做。 event.getChannel().sendMessage("Hello World").queue(); 我想知道是否可以将 channel
我是一名优秀的程序员,十分优秀!