Client data refresh QWebSocket passive network module (D)

A brief
WebSocket is a Web-based protocol, designed to enable two-way communication between a client application and a remote host. If the initial handshake is successful, the two entities can send data back and forth. WebSocket is to try to get by with less delay and minimum data exchange network application solutions for real-time data.
Second, the code

#ifndef WEBSOCKET_H
#define WEBSOCKET_H

#include <QObject>
#include <QWebSocket>
#include <QUrl>
#include <QDebug>
#include <QTimer>
class webSocketMgr : public QObject
{
    Q_OBJECT
public:
    explicit webSocketMgr(QObject *parent = nullptr);
    ~webSocketMgr();
    void setUrl(const QUrl &url);
    void start();
private slots:
    void on_connected();
    void on_disconnected();
    void on_error(QAbstractSocket::SocketError error);
    void on_textFrameReceived(const QString &frame, bool isLastFrame);
    void on_textMessageReceived(const QString &message);
    void on_timeout();
private:
    void init();
private:
    QUrl m_url;
    QWebSocket m_socket;
    QTimer m_timer;
};

#endif // WEBSOCKET_H

#include "WebSocketMgr.h"
webSocketMgr::webSocketMgr(QObject *parent):
    QObject(parent)
{
    init();
}
webSocketMgr::~webSocketMgr()
{

}
**url形式      //ws://192.168.0.17:60000/data** 
void webSocketMgr::setUrl(const QUrl &url)
{
    m_url = url;
}

void webSocketMgr::start()
{
    m_socket.open(m_url);
}

void webSocketMgr::init()
{
    connect(&m_socket,SIGNAL(connected()),
            this,SLOT(on_connected()));
    connect(&m_socket,SIGNAL(disconnected()),
            this,SLOT(on_disconnected()));
    connect(&m_socket,SIGNAL(error(QAbstractSocket::SocketError)),
            this,SLOT(on_error(QAbstractSocket::SocketError)));
    connect(&m_socket,SIGNAL(textFrameReceived(QString,bool)),
            this,SLOT(on_textFrameReceived(QString,bool)));
    connect(&m_socket,SIGNAL(textMessageReceived(QString)),
            this,SLOT(on_textMessageReceived(QString)));
    connect(&m_timer,SIGNAL(timeout()),
            this,SLOT(on_timeout()));
}

void webSocketMgr::on_connected()
{
    qDebug()<<"webSocketMgr::on_connected";
}
void webSocketMgr::on_disconnected()
{
    m_socket.close();
    qDebug()<<"webSocketMgr::on_disconnected";

}
void webSocketMgr::on_error(QAbstractSocket::SocketError error)
{
    qDebug()<<"webSocketMgr::on_error"<<error;
}
void webSocketMgr::on_textFrameReceived(const QString &frame, bool isLastFrame)
{
    qDebug()<<"webSocketMgr::on_textFrameReceived"<<frame<<isLastFrame;
}
void webSocketMgr::on_textMessageReceived(const QString &message)
{
    qDebug()<<"webSocketMgr::on_textMessageReceived"<<message;
    //此处解析服务端推动的消息,消息体一般为json,便于扩展
}
void webSocketMgr::on_timeout()
{
    qDebug()<<"webSocketMgr::on_timeout";
    //ping服务端,通知其该客户端online
    m_socket.ping("ping ping ping");
}

Published 30 original articles · won praise 1 · views 1141

Guess you like

Origin blog.csdn.net/u010906468/article/details/104882648