QT Websocket 서버 클라이언트 통신(서비스 섹션)

8879 단어
서버 클라이언트 통신을 위한 QT Websocket(클라이언트 섹션)
https://blog.csdn.net/qq_39573345/article/details/80682232
Serverdialog.cc
#include "Serverdialog.hpp"
#include 
#include 
#include 
#include 
#include 
#include 
#include 

ServerDialog::ServerDialog( QWidget *parent)
    : QWidget(parent)

{
    //qhboxLayout1 
    QLabel* monitorLabel= new QLabel(" ");
    m_monitorSpinBox = new QSpinBox();
    m_monitorSpinBox->setRange(0,65535);
    m_startButton = new QPushButton(" ");
    m_stopButton = new QPushButton(" ");
    m_stopButton->setEnabled(false);
    QHBoxLayout *qhboxLayout1 = new QHBoxLayout;
    qhboxLayout1->addWidget(monitorLabel);
    qhboxLayout1->addWidget(m_monitorSpinBox);
    qhboxLayout1->addWidget(m_startButton);
    qhboxLayout1->addWidget(m_stopButton);

    //qhboxLayout2 
    QLabel* sendLabel=new QLabel(" ");
    QHBoxLayout *qhboxLayout2 = new QHBoxLayout;
    qhboxLayout2->addWidget(sendLabel);

    //qhboxLayout3 
    m_sendTextedit = new QTextEdit;
    m_sendTextedit->setEnabled(false);
    m_sendTextedit->setFixedHeight(50);
    m_sendButton= new QPushButton(" ");
    m_sendButton->setEnabled(false);
    m_sendButton->setFixedHeight(50);

    QHBoxLayout *qhboxlayout3 = new QHBoxLayout;
    qhboxlayout3->addWidget(m_sendTextedit,2);
    qhboxlayout3->addWidget(m_sendButton,1);

    //qvboxlayout411
    QLabel* receiveLabel = new QLabel(" ");
    m_receiveTextEdit = new QTextEdit();
    m_receiveTextEdit->setReadOnly(true);

    QVBoxLayout *qvboxlayout411 = new QVBoxLayout;
    qvboxlayout411->addWidget(receiveLabel);
    qvboxlayout411->addWidget(m_receiveTextEdit);

    //qhboxlayout412
    m_cleanButton = new QPushButton(" ");
    m_cleanButton->setEnabled(false);
    QHBoxLayout *qhboxlayout412 = new QHBoxLayout;
    qhboxlayout412->addStretch();
    qhboxlayout412->addWidget(m_cleanButton);

    //qvboxlayout41
    QVBoxLayout *qvboxlayout41 = new QVBoxLayout;
    qvboxlayout41->addLayout(qvboxlayout411);
    qvboxlayout41->addLayout(qhboxlayout412);

    //qvboxlayout42
    QLabel* linkclientLabel=new QLabel(" ");
    m_linkclientListWidget=new QListWidget;
    QVBoxLayout* qvboxlayout42 = new QVBoxLayout;
    qvboxlayout42->addWidget(linkclientLabel);
    qvboxlayout42->addWidget(m_linkclientListWidget);

    //qvboxlayout4
    QHBoxLayout *qhboxlayout4 = new QHBoxLayout;
    qhboxlayout4->addLayout(qvboxlayout41,2);
    qhboxlayout4->addLayout(qvboxlayout42,1);

    //mainlayout
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(qhboxLayout1,1);
    mainLayout->addLayout(qhboxLayout2,0.5);
    mainLayout->addLayout(qhboxlayout3,1);
    mainLayout->addLayout(qhboxlayout4,3);

    this->setLayout(mainLayout);
    this->setWindowTitle("Websocket Server");

    m_WebSocketServer=new QWebSocketServer("server",QWebSocketServer::NonSecureMode);
    connect(m_WebSocketServer,SIGNAL(newConnection()),this,SLOT(onNewConnection()));
    connect(m_startButton,SIGNAL(clicked(bool)),this,SLOT(onStartButtonClick()));
    connect(m_stopButton,SIGNAL(clicked(bool)),this,SLOT(onStopButtonClick()));
    connect(m_cleanButton,SIGNAL(clicked(bool)),this,SLOT(onCleanButtonClick()));
    connect(m_sendButton,SIGNAL(clicked(bool)),this,SLOT(onSendButtonClick()));
}

ServerDialog::~ServerDialog()
{
}

void ServerDialog::onStartButtonClick(){
    int i_port = m_monitorSpinBox->text().toInt();
    m_WebSocketServer->listen(QHostAddress::Any,i_port);
    m_startButton->setEnabled(false);
    m_stopButton->setEnabled(true);
    qDebug()<isListening();
    qDebug()<serverPort();
    qDebug()<serverAddress();
}
void ServerDialog::onStopButtonClick(){
    m_startButton->setEnabled(true);
    m_stopButton->setEnabled(false);
    m_WebSocketServer->close();
}
void ServerDialog::onSendButtonClick(){
    QString msg = m_sendTextedit->document()->toPlainText();
    pSocket->sendTextMessage(msg);
}
void ServerDialog::onCleanButtonClick(){
    m_receiveTextEdit->clear();
}
// 
void ServerDialog::onNewConnection(){

    qDebug() << "hello";
    m_startButton->setEnabled(false);
    m_stopButton->setEnabled(true);
    m_sendTextedit->setEnabled(true);
    m_sendButton->setEnabled(true);
    m_cleanButton->setEnabled(true);

    pSocket = m_WebSocketServer->nextPendingConnection();
   
    connect(pSocket,SIGNAL(textMessageReceived(QString)),this,SLOT(processTextMessage(QString)));
    connect(pSocket,SIGNAL(disconnected()),this,SLOT(socketDisconnected()));

    QString item = pSocket->peerAddress().toString();
    m_linkclientListWidget->addItem(item);
    m_clients << pSocket;
}
// 
void ServerDialog::processTextMessage(QString message){
    QString time = current_date_time->currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz ddd");
    QString item = pSocket->peerAddress().toString();
    m_receiveTextEdit->append(time +""+ item + "
" + message); } // void ServerDialog::socketDisconnected(){ m_startButton->setEnabled(true); m_stopButton->setEnabled(false); m_sendButton->setEnabled(false); m_sendTextedit->setEnabled(false); m_receiveTextEdit->setEnabled(false); m_cleanButton->setEnabled(false); }
Serverdialog.hpp
#ifndef WEBSOCKEDSERVERDIALOG_HPP
#define WEBSOCKEDSERVERDIALOG_HPP

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "qwebsocketserver.hpp"
#include "qwebsocket.hpp"
#include 
class ServerDialog : public QWidget
{
    Q_OBJECT

public:
    explicit ServerDialog(QWidget *parent = 0);

    ~ServerDialog();
public slots:
    void onStartButtonClick();
    void onStopButtonClick();
    void onSendButtonClick();
    void onCleanButtonClick();
Q_SIGNALS:
    void closed();
private Q_SLOTS:
    void onNewConnection();
    void processTextMessage(QString message);
    void socketDisconnected();
    //void processBinaryMessage(QByteArray message);

private:
    QSpinBox* m_monitorSpinBox;
    QPushButton* m_startButton;
    QPushButton* m_stopButton;
    QTextEdit* m_sendTextedit;
    QPushButton* m_sendButton;
    QListWidget* m_linkclientListWidget;
    QPushButton* m_cleanButton;
    QTextEdit* m_receiveTextEdit;

    QWebSocketServer * m_WebSocketServer;
    QList m_clients;
    bool m_debug;
    QWebSocket *pSocket;
    QDateTime *current_date_time;

};

#endif // WEBSOCKEDSERVERDIALOG_HPP

main.cc
#include "Serverdialog.hpp"
#include 
#include 
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QDir::setCurrent(QApplication::applicationDirPath());
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));//set QString codec
    ServerDialog w;
    w.show();
    return a.exec();
}
WeebsockedServer.pro 여기서 프로젝트를 만들 때 e를 더 쳤지만 영향을 주지 않습니다 =.=
#-------------------------------------------------
#
# Project created by QtCreator 2018-05-10T10:16:25
#
#-------------------------------------------------

QT       += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = WeebsockedServer
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS QT_VERSION_LESS_FIVE

INCLUDEPATH += $$PWD/../QtWebsocket

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cc \
    Serverdialog.cc

HEADERS += \
    Serverdialog.hpp

DESTDIR = $$PWD/../Bin

LIBS +=-L$$DESTDIR -lQtWebsocket

좋은 웹페이지 즐겨찾기