接上文QT Websocket实现服务器客户端通信(客户端部分)
https://blog.csdn.net/qq_39573345/article/details/80682232
Serverdialog.cc
#include "Serverdialog.hpp" #include <QGridLayout> #include <QVBoxLayout> #include <QHBoxLayout> #include <QLabel> #include <QWidget> #include <QDebug> #include <QByteArray> 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()<<m_WebSocketServer->isListening(); qDebug()<<m_WebSocketServer->serverPort(); qDebug()<<m_WebSocketServer->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 + "\n" + 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 <QDialog> #include <QLineEdit> #include <QTextEdit> #include <QListWidget> #include <QList> #include <QListWidgetItem> #include <QTableWidget> #include <QPushButton> #include <QSpinBox> #include "qwebsocketserver.hpp" #include "qwebsocket.hpp" #include <QTime> 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<QWebSocket *> m_clients; bool m_debug; QWebSocket *pSocket; QDateTime *current_date_time; }; #endif // WEBSOCKEDSERVERDIALOG_HPP
main.cc
#include "Serverdialog.hpp" #include <QApplication> #include <QDir> #include <QTextCodec> 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