【xxx.pro】
QT += websockets
【mainwindow.h】
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "ui_mainwindow.h"
#include "QWebSocketServer"
#include "QHostAddress"
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QWebSocketServer *server;
QHostAddress *addr;
};
#endif // MAINWINDOW_H
【mainwindow.cpp】
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
addr = new QHostAddress("0.0.0.0");
server = new QWebSocketServer("ws_server_name",QWebSocketServer::SslMode::NonSecureMode,this);
server->listen(*addr,8080);
connect(server,QWebSocketServer::newConnection,this,[]() {
qDebug() << "new connect";
});
}
MainWindow::~MainWindow() {
delete ui;
delete server;
delete addr;
}
标签:QT,server,MainWindow,ws,服务器,include,mainwindow,QWebSocketServer
From: https://www.cnblogs.com/hhddd-1024/p/17842952.html