当前位置:网站首页>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
边栏推荐
- JVM memory and memory overflow exceptions (personal summary)
- 如果我是pm之 演出电影vr购票展示
- egg测试的知识大全--mock、superTest、coffee
- Membarrier (personal learning and understanding)
- Necessity of selenium preloading cookies
- Parsing of string class intern() method
- Laravel database
- Knowledge of egg testing -- mock, Supertest, coffee
- Uncle wolf is looking for a translator -- Plato -- ongoing translation
- Laravel [view]
猜你喜欢
Necessity of selenium preloading cookies
Modèle axé sur le domaine DDD (III) - gestion des transactions à l'aide de Saga
4 most common automated test challenges and Countermeasures
How to set the initial value of El input number to null
2021-09-28
Redis的基本知识
phphphphphphphp
CPT 104_ TTL 09
Double click The jar package cannot run the solution
创建进程内存管理copy_mm - 进程与线程(九)
随机推荐
what is wifi6?
Source code analysis of how to use jump table in redis
Laravel routing settings
The introduction of lean management needs to achieve these nine points in advance
Various situations of data / component binding
CPT 104_ TTL 09
If the route reports an error after deployment according to the framework project
領域驅動模型DDD(三)——使用Saga管理事務
egg的static的前缀是可以修改惹,靴靴
d. TS --- for more detailed knowledge, please refer to the introduction on the official website (chapter of declaration document)
Modèle axé sur le domaine DDD (III) - gestion des transactions à l'aide de Saga
Escape characters \ splicing of data formats
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
可執行程序執行流程
跨域CORS的情缘~
2021-09-28
弘玑Cyclone RPA为国金证券提供技术支撑,超200个业务场景实现流程自动化
Three 之 three.js (webgl)简单实现根据点绘制线/弧线(基于LineGeometry / Line2 / LineMaterial,绘制两点基于圆心的弧线段)
Three of three JS (WEB GL) model deletion / scene emptying / simple sorting of memory release
selenium預先加載cookie的必要性