赞
踩
QWebSocketServer以QTcpServer为模型,行为相同,我们对QTcpServer使用方法有了解的,会很快对该类上手。
enum QWebSocketServer::SslMode:指示服务器运行模式。
SecureMode:服务器以安全模式运行(wss)。
NonSecureMode:服务器以非安全模式运行(ws)。
简介TCP三次握手:
服务端:
头文件
- #ifndef QWEBSOCKETWIDGET_H
- #define QWEBSOCKETWIDGET_H
-
- #include <QWidget>
- #include <QWebSocket>
- #include <QDateTime>
- QT_BEGIN_NAMESPACE
- namespace Ui { class QWebSocketWidget; }
- QT_END_NAMESPACE
-
- class QWebSocketWidget : public QWidget
- {
- Q_OBJECT
-
- public:
- QWebSocketWidget(QWidget *parent = nullptr);
- ~QWebSocketWidget();
-
- private slots:
- void on_ConnectBtn_clicked();
-
- void on_disConnectBtn_clicked();
-
- void on_sendDataBtn_clicked();
-
- void on_clearlogDataBtn_clicked();
-
- void onDisconnected();
- void onConnected();
- void onTextMessageRecived(QString str);
-
- private:
- QWebSocket Websocket;
-
- private:
- Ui::QWebSocketWidget *ui;
- };
- #endif // QWEBSOCKETWIDGET_H
源文件
- #include "qwebsocketwidget.h"
- #include "ui_qwebsocketwidget.h"
-
- QWebSocketWidget::QWebSocketWidget(QWidget *parent)
- : QWidget(parent)
- , ui(new Ui::QWebSocketWidget)
- {
- ui->setupUi(this);
- //连接信号和槽函数
- setWindowTitle("客户端");
- connect(&Websocket,&QWebSocket::disconnected,this,&QWebSocketWidget::onDisconnected);//客户端断开连接
- connect(&Websocket,&QWebSocket::connected,this,&QWebSocketWidget::onConnected);//客户端连接服务端
- connect(&Websocket,&QWebSocket::textMessageReceived,this,&QWebSocketWidget::onTextMessageRecived);//客户端收到服务端消息
-
- }
-
- QWebSocketWidget::~QWebSocketWidget()
- {
- delete ui;
- }
-
- //连接服务端
- void QWebSocketWidget::on_ConnectBtn_clicked()
- {
- ui->LogtextEdit->setText(QDateTime::currentDateTime().toString()+":开始连接");
- QString urlStr = QString("ws://%1").arg(ui->portlineEdit->text());//地址和端口
- Websocket.open(QUrl(urlStr));//套接字获得地址和端口的属性
- }
- //断开服务端
- void QWebSocketWidget::on_disConnectBtn_clicked()
- {
- Websocket.close();
- }
- //发送数据到服务端
- void QWebSocketWidget::on_sendDataBtn_clicked()
- {
- QString str = QDateTime::currentDateTime().toString()+":给服务端发送消息:"+ui->SendDatatextEdit->toPlainText();
- ui->LogtextEdit->setText(str);
- Websocket.sendTextMessage(ui->SendDatatextEdit->toPlainText());//给服务端发消息
-
- }
- //清空日志信息
- void QWebSocketWidget::on_clearlogDataBtn_clicked()
- {
- ui->LogtextEdit->clear();
- }
-
- void QWebSocketWidget::onDisconnected()
- {
- QString str = QDateTime::currentDateTime().toString()+"客户端断开连接!";
- ui->LogtextEdit->setText(str);
- }
-
- void QWebSocketWidget::onConnected()
- {
- QString str = QDateTime::currentDateTime().toString()+":客户端连接服务端!";
- ui->LogtextEdit->setText(str);
- }
-
- void QWebSocketWidget::onTextMessageRecived(QString str)
- {
- QString str1 = QDateTime::currentDateTime().toString()+":服务端发来消息:"+str;
- ui->LogtextEdit->setText(str1);
- }
客户端:
头文件
- #ifndef WEBSOCKETSERVERWIDGET_H
- #define WEBSOCKETSERVERWIDGET_H
-
- #include <QWidget>
- #include <QWebSocketServer>
- #include <QWebSocket>
- #include <QDateTime>
- QT_BEGIN_NAMESPACE
- namespace Ui { class webSocketServerWidget; }
- QT_END_NAMESPACE
-
- class webSocketServerWidget : public QWidget
- {
- Q_OBJECT
-
- public:
- webSocketServerWidget(QWidget *parent = nullptr);
- ~webSocketServerWidget();
-
- private slots:
- void on_StartListen_clicked();//开始监听
- void on_endListen_clicked();//结束监听
- void OnNewConnection();//新连接接入服务端
- void onTextMessageReived(QString str);//接受客户端发送的消息
- void onDisconnected();//客户端断开连接给服务端发消息
-
- void on_sendData_clicked();
-
- void on_clearLog_clicked();
-
- private:
- QWebSocketServer* socketServer;//服务端
- QWebSocket* websocket;//客户端
-
-
- private:
- Ui::webSocketServerWidget *ui;
- };
- #endif // WEBSOCKETSERVERWIDGET_H
源文件
- #include "websocketserverwidget.h"
- #include "ui_websocketserverwidget.h"
-
- webSocketServerWidget::webSocketServerWidget(QWidget *parent)
- : QWidget(parent)
- , ui(new Ui::webSocketServerWidget)
- {
- ui->setupUi(this);
- setWindowTitle("服务端");
- socketServer = new QWebSocketServer("Server",QWebSocketServer::NonSecureMode);//采用非安全模式
- //客户端有新连接接入时,服务端会触发这个信号
- connect(socketServer,&QWebSocketServer::newConnection,this,&webSocketServerWidget::OnNewConnection);
- }
-
- webSocketServerWidget::~webSocketServerWidget()
- {
- delete ui;
- }
-
-
- void webSocketServerWidget::on_StartListen_clicked()
- {
- if(ui->StartListen->text().isEmpty())
- return;
- int port = ui->lineEditPort->text().toInt();
- socketServer->listen(QHostAddress::Any,port);//服务端套接字监听(ip地址,端口)
- ui->logTextLine->setText(QDateTime::currentDateTime().toString()+":开始监听");
-
- }
-
- void webSocketServerWidget::OnNewConnection()
- {
- websocket = socketServer->nextPendingConnection();
- connect(websocket,&QWebSocket::textMessageReceived,this,&webSocketServerWidget::onTextMessageReived);
- connect(websocket,&QWebSocket::disconnected,this,&webSocketServerWidget::onDisconnected);
- }
-
- void webSocketServerWidget::onTextMessageReived(QString str)
- {
- ui->logTextLine->setText(QDateTime::currentDateTime().toString()+":有新的连接进入,"+str);
- }
-
- void webSocketServerWidget::onDisconnected()
- {
- ui->logTextLine->setText(QDateTime::currentDateTime().toString()+"客户端断开连接");
- }
-
- void webSocketServerWidget::on_endListen_clicked()
- {
- ui->log->setText(QDateTime::currentDateTime().toString()+":停止监听");
- if(socketServer->isListening())
- socketServer->close();
- }
-
- void webSocketServerWidget::on_sendData_clicked()
- {
- QString msg = ui->textEditSendData->toPlainText();
- websocket->sendTextMessage(msg);
- ui->logTextLine->setText(QDateTime::currentDateTime().toString()+":服务端发送消息:"+ui->textEditSendData->toPlainText());
- }
-
- void webSocketServerWidget::on_clearLog_clicked()
- {
- ui->logTextLine->clear();
- }
资源链接
Qt网络编程QWebSocketServer和QWebSocket客户端和网络端demo-C++文档类资源-CSDN下载
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。