Qt【WebSocket】创建一个简单的websocket连接

1、使用说明
pro文件中:

QT += websockets
C++类文件中:

#include <QWebSocket>
2、客户端的demo
该demo实现的功能:

1、创建webSocket连接;

2、连接中断后,自动发起重连,周期为3秒。

这里新建一个类,类名为DataReceive。

.h文件

/**
 * @breaf 用于创建websocket连接
 * @note  该类实现了client端基本的websocket创建,自动重连。
 * @author  LittleMushroom
 * date   2018年5月15日
 */
 
#ifndef DATARECEIVE_H
#define DATARECEIVE_H
 
#include <QtCore/QObject>
#include <QWebSocket>
#include <QString>
#include <QTimer>
 
class DataReceive : public QObject
{
    Q_OBJECT
public:
    DataReceive(QObject *parent=0);
    ~DataReceive();
 
public slots:
    void createDataRecvWS();    /*-<创建websocket连接 */
 
private:
    QWebSocket *dataRecvWS;     /*-<websocket类 */
    bool connectStatus;         /*-<websocket连接状态,连接成功:true;断开:false */
    void reconnect();           /*-<周期重连函数 */
    QTimer *m_timer;            /*-<周期重连Timer */
 
 
private slots:
    void onConnected();                 /*-<socket建立成功后,触发该函数 */
    void onTextReceived(QString msg);   /*-<收到Sev端的数据时,触发该函数 */
    void onDisConnected();              /*-<socket连接断开后,触发该函数 */
};
 
#endif // DATARECEIVE_H
.cpp文件

#include "datareceive.h"
#include <QDebug>
 
DataReceive::DataReceive(QObject *parent):QObject(parent)
{
    dataRecvWS = Q_NULLPTR;
    connectStatus = false;
    m_timer = new QTimer();
}
 
DataReceive::~DataReceive(){
 
}
 
/**
 * @breaf 创建WebSocket连接
 */
void DataReceive::createDataRecvWS(){
    if(dataRecvWS == Q_NULLPTR){
        dataRecvWS = new QWebSocket();
        qDebug()<<"create websocket!";
        connect(dataRecvWS,&QWebSocket::disconnected,this,DataReceive::onDisConnected,Qt::AutoConnection);
        connect(dataRecvWS,&QWebSocket::textMessageReceived,this,DataReceive::onTextReceived,Qt::AutoConnection);
        connect(dataRecvWS,&QWebSocket::connected,this,DataReceive::onConnected,Qt::AutoConnection);
        connect(m_timer,m_timer->timeout,this,DataReceive::reconnect,Qt::AutoConnection);
        dataRecvWS->open(QUrl("ws://192.168.0.17:60000/data"));
    }
}
 
/**
 * @breaf 判断连接状态
 * @note  当连接成功后,触发该函数
 */
void DataReceive::onConnected(){
    qDebug()<<"DataReveive websocket is already connect!";
    connectStatus = true;
    m_timer->stop();
    qDebug()<<"Address:"<<dataRecvWS->peerAddress();
 
 
}
 
/**
 * @breaf 数据处理函数
 * @param msg,数据信息
 * @note  当收到服务端发送的数据时,触发该函数
 */
void DataReceive::onTextReceived(QString msg){
    qDebug()<<"----------------data-----------------";
    qDebug()<<msg;
}
 
/**
 * @breaf 连接断开
 * @note  当连接断开时,触发该函数
 */
void DataReceive::onDisConnected(){
    qDebug()<<"DataReceive websocket is disconnected!!!";
    connectStatus = false;
    m_timer->start(3000);/*-<当连接失败时,触发重连计时器,设置计数周期为3秒 */
}
 
/**
 * @breaf 周期重连函数
 * @note  连接建立成功时,调用该函数
 */
void DataReceive::reconnect(){
    qDebug()<<"try to reconnect!";
    dataRecvWS->abort();
    dataRecvWS->open(QUrl("ws://192.168.0.17:60000/data"));
}
main.cpp中的相关使用

DataReceive *m_dataReceive = new DataReceive();
m_dataReceive->createDataRecvWS();
--------------------- 
作者:踩星星的小蘑菇 
来源:CSDN 
原文:https://blog.csdn.net/dumuweiyang/article/details/80308431 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/lvxiong2007/article/details/83928908