Skip to content

Commit

Permalink
发送文本数据
Browse files Browse the repository at this point in the history
  • Loading branch information
muleimulei committed Aug 9, 2021
1 parent ea904fd commit c9c4512
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 16 deletions.
Binary file added 2.wav
Binary file not shown.
2 changes: 1 addition & 1 deletion CloudMeeting.pro
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

RESOURCES += \
img.qrc
resource.qrc
49 changes: 45 additions & 4 deletions mytcpsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ void MyTcpSocket::sendData(MESG* send)
{
if (_socktcp->state() == QAbstractSocket::UnconnectedState)
{
emit sendTextOver();
if (send->data) free(send->data);
if (send) free(send);
return;
Expand All @@ -89,7 +90,7 @@ void MyTcpSocket::sendData(MESG* send)
qToBigEndian<quint32>(ip, sendbuf + bytestowrite);
bytestowrite += 4;

if (send->msg_type == CREATE_MEETING || send->msg_type == AUDIO_SEND || send->msg_type == CLOSE_CAMERA || send->msg_type == IMG_SEND) //创建会议,发送音频,关闭摄像头,发送图片
if (send->msg_type == CREATE_MEETING || send->msg_type == AUDIO_SEND || send->msg_type == CLOSE_CAMERA || send->msg_type == IMG_SEND || send->msg_type == TEXT_SEND) //创建会议,发送音频,关闭摄像头,发送图片
{
//发送数据大小
qToBigEndian<quint32>(send->len, sendbuf + bytestowrite);
Expand Down Expand Up @@ -129,6 +130,12 @@ void MyTcpSocket::sendData(MESG* send)

_socktcp->waitForBytesWritten();

if(send->msg_type == TEXT_SEND)
{
emit sendTextOver(); //成功往内核发送文本信息
}


if (send->data)
{
free(send->data);
Expand Down Expand Up @@ -162,7 +169,7 @@ void MyTcpSocket::run()
//构造消息体
MESG * send = queue_send.pop_msg();
if(send == NULL) continue;
QMetaObject::invokeMethod(this, "sendData", Q_ARG(MESG *, send));
QMetaObject::invokeMethod(this, "sendData", Q_ARG(MESG*, send));
}
}

Expand Down Expand Up @@ -326,7 +333,7 @@ void MyTcpSocket::recvFromSocket()
}
}
}
else if (msgtype == IMG_RECV || msgtype == PARTNER_JOIN || msgtype == PARTNER_EXIT || msgtype == AUDIO_RECV || msgtype == CLOSE_CAMERA)
else if (msgtype == IMG_RECV || msgtype == PARTNER_JOIN || msgtype == PARTNER_EXIT || msgtype == AUDIO_RECV || msgtype == CLOSE_CAMERA || msgtype == TEXT_RECV)
{
//read ipv4
quint32 ip;
Expand Down Expand Up @@ -407,6 +414,7 @@ void MyTcpSocket::recvFromSocket()
msg->data = (uchar*)malloc(rdc.size());
if (msg->data == nullptr)
{
free(msg);
qDebug() << __LINE__ << "malloc msg.data failed";
}
else
Expand All @@ -420,7 +428,40 @@ void MyTcpSocket::recvFromSocket()
}
}
}
}
else if(msgtype == TEXT_RECV)
{
//解压缩
QByteArray cc((char *)recvbuf + MSG_HEADER, data_size);
std::string rr = qUncompress(cc).toStdString();
if(rr.size() > 0)
{
MESG* msg = (MESG*)malloc(sizeof(MESG));
if (msg == NULL)
{
qDebug() << __LINE__ << "malloc failed";
}
else
{
memset(msg, 0, sizeof(MESG));
msg->msg_type = TEXT_RECV;
msg->ip = ip;
msg->data = (uchar*)malloc(rr.size());
if (msg->data == nullptr)
{
free(msg);
qDebug() << __LINE__ << "malloc msg.data failed";
}
else
{
memset(msg->data, 0, rr.size());
memcpy_s(msg->data, rr.size(), rr.data(), rr.size());
msg->len = rr.size();
queue_recv.push_msg(msg);
}
}
}
}
}
}
else
{
Expand Down
1 change: 1 addition & 0 deletions mytcpsocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public slots:
void errorDetect(QAbstractSocket::SocketError error);
signals:
void socketerror(QAbstractSocket::SocketError);
void sendTextOver();
};

#endif // MYTCPSOCKET_H
12 changes: 8 additions & 4 deletions mytextedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ void MyTextEdit::changeCompletion(QString text)
tc.insertText(" ");
edit->setTextCursor(tc);

ipspan.push_back(QPair<int, int>(pos, str.size()));
ipspan.push_back(QPair<int, int>(pos, str.size()+1));

}

QString MyTextEdit::toPlainText()
Expand Down Expand Up @@ -117,8 +118,6 @@ bool MyTextEdit::eventFilter(QObject *obj, QEvent *event)
if(event->type() == QEvent::KeyPress)
{
QKeyEvent *keyevent = static_cast<QKeyEvent *>(event);
if(keyevent->key() == Qt::Key_Backspace || keyevent->key() == Qt::Key_Delete)
{
QTextCursor tc = edit->textCursor();
int p = tc.position();
int i;
Expand All @@ -139,8 +138,13 @@ bool MyTextEdit::eventFilter(QObject *obj, QEvent *event)
ipspan.removeAt(i);
return true;
}
else if(p >= ipspan[i].first && p <= ipspan[i].second)
{
QTextCursor tc = edit->textCursor();
tc.setPosition(ipspan[i].second);
edit->setTextCursor(tc);
}
}
}
}
}
return QWidget::eventFilter(obj, event);
Expand Down
3 changes: 3 additions & 0 deletions img.qrc → resource.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
<file>1.jpg</file>
<file>3.gif</file>
</qresource>
<qresource prefix="/myEffect">
<file>2.wav</file>
</qresource>
</RCC>
21 changes: 20 additions & 1 deletion sendtext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,27 @@ void SendText::run()
queue_send.push_msg(send);
}
}
else if(text.type == TEXT_SEND)
{
send->msg_type = TEXT_SEND;
QByteArray data = qCompress(QByteArray::fromStdString(text.str.toStdString())); //压缩
send->len = data.size();
send->data = (uchar *) malloc(send->len);
if(send->data == NULL)
{
WRITE_LOG("malloc error");
qDebug() << __FILE__ << __LINE__ << "malloc error";
free(send);
continue;
}
else
{
memset(send->data, 0, send->len);
memcpy_s(send->data, send->len, data.data(), data.size());
queue_send.push_msg(send);
}
}
}

}
}
void SendText::stopImmediately()
Expand Down
36 changes: 31 additions & 5 deletions widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <QDateTime>
#include <QCompleter>
#include <QStringListModel>
#include <QSound>
QRect Widget::pos = QRect(-1, -1, -1, -1);

extern LogQueue *logqueue;
Expand All @@ -26,6 +27,8 @@ Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{


//开启日志线程
logqueue = new LogQueue();
logqueue->start();
Expand Down Expand Up @@ -56,7 +59,7 @@ Widget::Widget(QWidget *parent)
ui->createmeetBtn->setDisabled(true);
ui->openAudio->setDisabled(true);
ui->openVedio->setDisabled(true);
// ui->sendmsg->setDisabled(true);
ui->sendmsg->setDisabled(true);
mainip = 0; //主屏幕显示的用户IP图像

//-------------------局部线程----------------------------
Expand All @@ -73,6 +76,7 @@ Widget::Widget(QWidget *parent)

//数据处理(局部线程)
_mytcpSocket = new MyTcpSocket(); // 底层线程专管发送
connect(_mytcpSocket, SIGNAL(sendTextOver()), this, SLOT(textSend()));
//connect(_mytcpSocket, SIGNAL(socketerror(QAbstractSocket::SocketError)), this, SLOT(mytcperror(QAbstractSocket::SocketError)));


Expand Down Expand Up @@ -136,8 +140,6 @@ Widget::Widget(QWidget *parent)
te_font.setPointSize(12);

ui->listWidget->setFont(te_font);


iplist << "@192.168.1.1" << "@192.168.1.112" << "@192.167.34.12";

ui->plainTextEdit->setCompleter(iplist);
Expand Down Expand Up @@ -503,6 +505,20 @@ void Widget::datasolve(MESG *msg)
}
repaint();
}
else if(msg->msg_type == TEXT_RECV)
{
QString str = QString::fromStdString(std::string((char *)msg->data, msg->len));
qDebug() << str;
QString time = QString::number(QDateTime::currentDateTimeUtc().toTime_t());
ChatMessage *message = new ChatMessage(ui->listWidget);
QListWidgetItem *item = new QListWidgetItem();
dealMessageTime(time);
dealMessage(message, item, str, time, QHostAddress(msg->ip).toString() ,ChatMessage::User_She);
if(str.contains('@' + QHostAddress(_mytcpSocket->getlocalip()).toString()))
{
QSound::play(":/myEffect/2.wav");
}
}
else if(msg->msg_type == PARTNER_JOIN)
{
Partner* p = addPartner(msg->ip);
Expand Down Expand Up @@ -588,6 +604,7 @@ Partner* Widget::addPartner(quint32 ip)
connect(this, SIGNAL(volumnChange(int)), _ainput, SLOT(setVolumn(int)), Qt::UniqueConnection);
connect(this, SIGNAL(volumnChange(int)), _aoutput, SLOT(setVolumn(int)), Qt::UniqueConnection);
ui->openAudio->setDisabled(false);
ui->sendmsg->setDisabled(false);
_aoutput->startPlay();
}
return p;
Expand Down Expand Up @@ -706,7 +723,6 @@ void Widget::speaks(QString ip)
void Widget::on_sendmsg_clicked()
{
QString msg = ui->plainTextEdit->toPlainText().trimmed();

if(msg.size() == 0)
{
qDebug() << "empty";
Expand All @@ -720,6 +736,7 @@ void Widget::on_sendmsg_clicked()
dealMessageTime(time);
dealMessage(message, item, msg, time, "192.169.1.111" ,ChatMessage::User_Me);
emit PushText(TEXT_SEND, msg);
ui->sendmsg->setDisabled(true);
}

void Widget::dealMessage(ChatMessage *messageW, QListWidgetItem *item, QString text, QString time, QString ip ,ChatMessage::User_Type type)
Expand Down Expand Up @@ -749,11 +766,20 @@ void Widget::dealMessageTime(QString curMsgTime)
if(isShowTime) {
ChatMessage* messageTime = new ChatMessage(ui->listWidget);
QListWidgetItem* itemTime = new QListWidgetItem();

ui->listWidget->addItem(itemTime);
QSize size = QSize(ui->listWidget->width() , 40);
messageTime->resize(size);
itemTime->setSizeHint(size);
messageTime->setText(curMsgTime, curMsgTime, size);
ui->listWidget->setItemWidget(itemTime, messageTime);
}
}

void Widget::textSend()
{
qDebug() << "send text over";
QListWidgetItem* lastItem = ui->listWidget->item(ui->listWidget->count() - 1);
ChatMessage* messageW = (ChatMessage *)ui->listWidget->itemWidget(lastItem);
messageW->setTextSuccess();
ui->sendmsg->setDisabled(false);
}
2 changes: 2 additions & 0 deletions widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ private slots:

void on_sendmsg_clicked();

void textSend();

signals:
void pushImg(QImage);
void PushText(MSG_TYPE, QString = "");
Expand Down
2 changes: 1 addition & 1 deletion widget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@
<enum>QTabWidget::Rounded</enum>
</property>
<property name="currentIndex">
<number>1</number>
<number>0</number>
</property>
<property name="usesScrollButtons">
<bool>true</bool>
Expand Down

0 comments on commit c9c4512

Please sign in to comment.