简体   繁体   English

在QT中的两个窗口之间共享WebSocket连接

[英]Share websocket connection between two windows in QT

I've created a class called CSocket: 我创建了一个名为CSocket的类:

CSocket.h CSocket.h

#ifndef CSOCKET_H
#define CSOCKET_H

#include <QtCore/QObject>
#include <QtWebSockets/QWebSocket>

class CSocket : public QObject
{
    Q_OBJECT
public:
    explicit CSocket(QObject *parent = nullptr);
    void onConnect(const QUrl &url);
    void onSendMesssage(QString message);
signals:
    void closed();
private slots:
    void onConnected();
    void onTextMessageReceived(QString message);
private:
    QWebSocket m_webSocket;
    QUrl m_url;
};

#endif // CSOCKET_H

CSocket.cpp CSocket.cpp

#include "csocket.h"

#include <QtCore/QDebug>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QMessageBox>

QT_USE_NAMESPACE

CSocket::CSocket(QObject *parent) : QObject(parent)
{
}

void CSocket::onConnect(const QUrl &url)
{
    m_url = url;

    connect(&m_webSocket, &QWebSocket::connected, this, &CSocket::onConnected);
    connect(&m_webSocket, &QWebSocket::disconnected, this, &CSocket::closed);

    m_webSocket.open(QUrl(url));
}

void CSocket::onConnected()
{
    connect(&m_webSocket, &QWebSocket::textMessageReceived, this, &CSocket::onTextMessageReceived);
}

void CSocket::onTextMessageReceived(QString message)
{    
    QMessageBox::information(nullptr, "Answer", message, QMessageBox::Ok);
}

void CSocket::onSendMesssage(QString message)
{
    m_webSocket.sendTextMessage(message);
}

In main window (QWidget) i create a connection: 在主窗口(QWidget)中,我创建一个连接:

CSocket *socket = new CSocket;
socket->onConnect(QUrl(QStringLiteral("ws://localhost:8080")));

Now is the question: how can i share the connection to another QWidget or QDialog? 现在的问题是:如何共享与另一个QWidget或QDialog的连接? I just don't want to reconnect in the new window. 我只是不想在新窗口中重新连接。 Does someone know how to do it? 有人知道怎么做吗?

Assuming that within your entire application you only want a connection as indicated, an appropriate pattern would be the singleton: 假设在您的整个应用程序中,您只希望按照指示进行连接,那么一个合适的模式就是单例:

csocket.h csocket.h

#ifndef CSOCKET_H
#define CSOCKET_H

#include <QObject>
#include <QWebSocket>

class CSocket : public QObject
{
    Q_OBJECT
public:
    static CSocket *instance();
    void onConnect(const QUrl &url);
    void onSendMesssage(QString message);
signals:
    void closed();
private slots:
    void onConnected();
    void onTextMessageReceived(QString message);
private:
    static CSocket* m_instance;
    explicit CSocket(QObject *parent = nullptr);
    QWebSocket m_webSocket;
    QUrl m_url;
};

#endif // CSOCKET_H

csocket.cpp csocket.cpp

#include "csocket.h"

#include <QMessageBox>

CSocket* CSocket::m_instance = nullptr;

CSocket::CSocket(QObject *parent) : QObject(parent)
{
}

CSocket *CSocket::instance()
{
    if (m_instance == nullptr)
        m_instance = new CSocket;
    return m_instance;
}

void CSocket::onConnect(const QUrl &url)
{
    m_url = url;

    connect(&m_webSocket, &QWebSocket::connected, this, &CSocket::onConnected);
    connect(&m_webSocket, &QWebSocket::disconnected, this, &CSocket::closed);

    m_webSocket.open(QUrl(url));
}

void CSocket::onConnected()
{
    connect(&m_webSocket, &QWebSocket::textMessageReceived, this, &CSocket::onTextMessageReceived);
}

void CSocket::onTextMessageReceived(QString message)
{
    QMessageBox::information(nullptr, "Answer", message, QMessageBox::Ok);
}

void CSocket::onSendMesssage(QString message)
{
    m_webSocket.sendTextMessage(message);
}

So instead of using the constructor you should use the instance() method: 因此,应该使用instance()方法代替使用构造函数:

//mainwindow

CSocket *socket = CSocket::instance();
socket->onConnect(QUrl(QStringLiteral("ws://localhost:8080")));

// another window
CSocket *socket = CSocket::instance();

As you can see there will only be one CSocket that is shared by all the windows. 如您所见,所有窗口都将共享一个CSocket。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM