当前位置:网站首页>Qwebsocket communication
Qwebsocket communication
2022-04-23 05:28:00 【Oriental forgetfulness】
QWebsocket How to use and QTcpSocket Special like .
1、 Define a QWebSocketServer.
2、 monitor IP And port
m_webSocketServer->listen(QHostAddress(m_ipEdit->text()),m_portEdit->text().toInt());
3、 Create a signal slot to receive the client .
connect(m_webSocketServer,SIGNAL(newConnection()),this,SLOT(slotNewConnect()));
4、 If there is a client connection , Then deal with , If you get socket Connect , Associated receiving slot .
QWebSocket* socket = m_webSocketServer->nextPendingConnection();
connect(socket,&QWebSocket::disconnected,this,[=]()
{});
connect(socket,&QWebSocket::textMessageReceived,this,[=](QString text)
{});
connect(socket,&QWebSocket::binaryMessageReceived,this,[=](QByteArray array)
{};
Send text :
socket->sendTextMessage(m_sendEdit->text());
Send binary data :
socket->sendBinaryMessage("IMGEND!");
Send pictures :
QByteArray array;
QBuffer buffer(&array);
buffer.open(QIODevice::WriteOnly);
QPixmap pixmap(m_imgPath);
pixmap.save(&buffer,"png");
for (int i = 0; i < m_webSocketList.size() ;i++)
{
QWebSocket* socket = m_webSocketList.at(i);
QString head = "IMGSTART!|"+m_imgPath.split("/").last();
socket->sendBinaryMessage(head.toUtf8());
socket->sendBinaryMessage(array);
socket->sendBinaryMessage("IMGEND!");
}
Send a file :
m_blockSize = 64*1024;
QFileInfo fileinfo(m_filePath);
qint64 fileSize = fileinfo.size();
QString fileName = fileinfo.fileName();
QString head = QString("FILESTART!|%1#%2").arg(fileName).arg(fileSize);
QFile file(m_filePath);
file.open(QIODevice::ReadOnly);
qint64 sendSize = 0;
m_progressDialog->setRange(0,100);
m_progressDialog->setVisible(true);
qDebug() <<QStringLiteral(" Current client :")<<m_webSocketList.size();
for (int i = 0; i < m_webSocketList.size() ;i++)
{
QWebSocket* socket = m_webSocketList.at(i);
socket->sendBinaryMessage(head.toUtf8());
QSslConfiguration config;
config.setPeerVerifyMode(QSslSocket::VerifyNone);
config.setProtocol(QSsl::AnyProtocol);
socket->setSslConfiguration(config);
qint64 writesize = fileSize;
qint64 len = 0;
do{
QByteArray array = file.read(qMin(writesize,m_blockSize));
len = socket->sendBinaryMessage(array);
sendSize += len;
writesize -= len;
// Due to sending too fast , Increase the delay to prevent the buffer from crashing before it has time to clean up
if (sendSize%(100*1024*1024) == 0)
{
QEventLoop eventloop;
QTimer::singleShot(1000, &eventloop, SLOT(quit()));//3000 Express 3000ms, namely 3 second , It can be modified according to the actual situation , The others don't change
eventloop.exec();
}
m_progressDialog->setValue(sendSize/(fileSize/100));
qDebug() <<"sendsize:"<<sendSize;
}while(writesize>0);
if(sendSize == fileSize)// Inspection document information
{
qDebug() << QStringLiteral(" The file has been sent ");
socket->sendBinaryMessage("FILEEND!");
file.close();
}
}
receive data :``` Receive text
connect(socket,&QWebSocket::textMessageReceived,this,[=](QString text)
{
m_recEdit->append(QStringLiteral(" receive :")+text);
if (text == "Finish")
emit signalRecFinish();
qDebug()<<"------recv1"<<text;
});
``
receive data :``` Receive binary receipt
connect(socket,&QWebSocket::binaryMessageReceived,this,[=](QByteArray array){
// Just take the picture head
if (array.contains("IMGSTART!"))
{
m_rectype = IMGTYPE;
m_pixName = QString(array).split("|").last();
m_arr.clear();
}
// Just close the end of the picture
else if (array == "IMGEND!")
{
QPixmap pixmap;
pixmap.loadFromData(m_arr);
m_imgLab->setPixmap(pixmap.scaled(m_imgLab->size(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
m_pixName = m_pixName.remove("bak_");
pixmap.save(QApplication::applicationDirPath()+"/"+m_pixName);
m_arr.clear();
}
// Receive file header
else if (array.contains("FILESTART!"))
{
m_rectype = FILETYPE;
QString str = array;
str = str.split("|").last();
QString fileName = str.section("#", 0, 0);
m_fileSize = str.section("#", 1, 1).toDouble();
m_recvSize = 0;
m_file.setFileName(fileName.remove("bak_"));
// Open file
bool isOK = m_file.open(QIODevice::WriteOnly);
if(!isOK)
{
qDebug() << QStringLiteral(" fail to open file ");
}
m_progressDialog->setRange(0,100);
m_progressDialog->setVisible(true);
}
// Receive the end of the file
else if (array == "FILEEND!")
{
// if(m_recvSize == m_fileSize)
// {
qDebug() << QStringLiteral(" actual ")<< m_fileSize;
QMessageBox::information(this, QStringLiteral(" complete "), QStringLiteral(" File received successfully "));
m_file.close();
// }
}
else
{
// Receive binary data of pictures
if (m_rectype == IMGTYPE)
{
m_arr.append(array);
}
// Receive binary data of the file
else if (m_rectype == FILETYPE)
{
qint64 len = m_file.write(array);
m_recvSize += len;
qDebug() << QStringLiteral(" receive ") <<m_recvSize;
m_progressDialog->setValue(m_recvSize/(m_fileSize/100));
}
}
});
版权声明
本文为[Oriental forgetfulness]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220543306971.html
边栏推荐
- Graphics.FromImage报错“Graphics object cannot be created from an image that has an indexed pixel ...”
- Good test data management, in the end how to do?
- d. TS --- for more detailed knowledge, please refer to the introduction on the official website (chapter of declaration document)
- 使用宝塔+xdebug+vscode远程调试代码
- Devops life cycle, all you want to know is here!
- Top 25 Devops tools in 2021 (Part 2)
- Redis in node -- ioredis
- App Store年交易额100万美元只缴15%佣金,中小开发者心里很矛盾
- Laravel routing settings
- How to add beautiful code blocks in word | a very complete method to sort out and compare
猜你喜欢

Graphics.FromImage报错“Graphics object cannot be created from an image that has an indexed pixel ...”

Anti crawler (0): are you still climbing naked with selenium? You're being watched! Crack webdriver anti crawler

弘玑|数字化时代下,HR如何进行自我变革和组织变革?

Cloud computing and cloud native architecture design of openshift

Getting started with varnish

varnish入门

Laravel routing job

STL learning notes 0x0001 (container classification)
![[untitled] Notepad content writing area](/img/0a/4a3636025c3e0441f45c99e3c67b67.png)
[untitled] Notepad content writing area

2021-11-01
随机推荐
Use pagoda + Xdebug + vscode to debug code remotely
IPI interrupt
Laravel routing job
相机成像+单应性变换+相机标定+立体校正
what is wifi6?
五一劳动节期间什么理财产品会有收益?
Excel 2016 cannot open the file for the first time. Sometimes it is blank and sometimes it is very slow. You have to open it for the second time
CPT 104_TTL 09
Redis的基本知识
Create cells through JS (while loop)
Escape characters \ splicing of data formats
Good test data management, in the end how to do?
Fast application fuzzy search
d. TS --- for more detailed knowledge, please refer to the introduction on the official website (chapter of declaration document)
弘玑微课堂 | Cyclone RPA之“灵活的数字员工”执行器
分支与循环语句
Call the interface to get the time
Edit, cancel, pull up menu
Rog attack
Output string in reverse order