<repo_name>ShenGX1992/WebSocket<file_sep>/README.md
# WebSocket
WebSocket-transferClient:基于Qt编写WebSocket通讯客户端
<file_sep>/transferclient.cpp
#include "transferclient.h"
#include "ui_transferclient.h"

TransferClient::TransferClient(const QUrl &url, QWidget *parent) :
    QDialog(parent),
    m_url(url),
    ui(new Ui::TransferClient)
{
    ui->setupUi(this);
    setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
    setWindowModality(Qt::ApplicationModal);
    move((QApplication::desktop()->width() - width()) / 2,
         (QApplication::desktop()->height() - height()) / 2);
}

TransferClient::~TransferClient()
{
    delete ui;
}

void TransferClient::onConnected()
{
        ui->listWidget->addItem("WebSocket connected");
        qDebug() << "WebSocket connected";
    QObject::connect(&m_webSocket, &QWebSocket::textMessageReceived,
            this, &TransferClient::onTextMessageReceived);
}

void TransferClient::unConnected()
{
        ui->listWidget->addItem( "WebSocket connected failed");
        qDebug() << "WebSocket connected failed";
}

void TransferClient::closed()
{
    ui->listWidget->addItem( "WebSocket closed");
    qDebug() << "WebSocket closed";
    m_webSocket.close();
}

void TransferClient::onTextMessageReceived(QString message)
{
    QString RECstring = "Receive:";
    RECstring.append(message);
    ui->listWidget->addItem(RECstring);
        qDebug() << "Message received:" << message;
}
void TransferClient::on_pushButton_connect_clicked()
{
    QString URLstring = "WebSocket server:";
    URLstring.append(m_url.toString());
    ui->listWidget->addItem(URLstring);
    qDebug() << "WebSocket server:" << m_url;
    /*QObject::*/connect(&m_webSocket, &QWebSocket::connected, this, &TransferClient::onConnected);
    /*QObject::*/connect(&m_webSocket, &QWebSocket::disconnected, this, &TransferClient::unConnected);
    m_webSocket.open(QUrl(m_url));
}

void TransferClient::on_pushButton_disconnect_clicked()
{
    ui->listWidget->addItem( "WebSocket closed");
    qDebug() << "WebSocket closed";
    m_webSocket.close();
}

void TransferClient::on_pushButton_send_clicked()
{
    ui->listWidget->addItem("Text Send");
    m_webSocket.sendTextMessage(ui->lineEdit_input->text());
}

void TransferClient::on_pushButton_Quit_clicked()
{
    m_webSocket.close();
    close();
}
<file_sep>/transferclient.h
#ifndef TRANSFERCLIENT_H
#define TRANSFERCLIENT_H

#include <QDialog>
#include <QtCore/QObject>
#include <QtWebSockets/QWebSocket>
#include <QKeyEvent>
#include <QThread>
#include <QTimer>
#include <cmath>
#include <QDebug>
#include <QMouseEvent>
#include <QDesktopWidget>

namespace Ui {
class TransferClient;
}

class TransferClient : public QDialog
{
    Q_OBJECT

public:
    explicit TransferClient(const QUrl &url,QWidget *parent = 0);
    ~TransferClient();

private Q_SLOTS:
    void onConnected();
    void unConnected();
    void closed();
    void onTextMessageReceived(QString message);

    void on_pushButton_connect_clicked();

    void on_pushButton_disconnect_clicked();

    void on_pushButton_send_clicked();

    void on_pushButton_Quit_clicked();

private:
    Ui::TransferClient *ui;
    QWebSocket m_webSocket;
    QUrl m_url;
};

#endif // TRANSFERCLIENT_H
<file_sep>/main.cpp
#include "transferclient.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TransferClient w(QUrl(QStringLiteral("ws://localhost:1234")));// "ws://192.168.0.117:1234"
    w.show();

    return a.exec();
}
