1.pro 添加
QT += websockets
#ifndef MYWEBSOCKETSERVER_H #define MYWEBSOCKETSERVER_H #include <QObject> #include <QtWebSockets/QWebSocket> #include <QtWebSockets/QWebSocketServer> #include <QList> #include <QByteArray> class MyWebSocketServer : public QObject { Q_OBJECT public: explicit MyWebSocketServer(quint16 port, bool debug = false, QObject *parent = nullptr); ~MyWebSocketServer(); signals: void closed(); public slots: void onNewConnection(); void processTextMessage(QString message); void processBinaryMessage(QByteArray message); void socketDisconnected(); private: QWebSocketServer *m_pWebSocketServer; QList<QWebSocket *> m_clients; bool m_debug; }; #endif // MYWEBSOCKETSERVER_H
#include "mywebsocketserver.h" //! [constructor] MyWebSocketServer::MyWebSocketServer(quint16 port, bool debug, QObject *parent) : QObject(parent), m_pWebSocketServer(new QWebSocketServer(QStringLiteral("Echo Server"), QWebSocketServer::NonSecureMode, this)), m_debug(debug) { if (m_pWebSocketServer->listen(QHostAddress::Any, port)) { if (m_debug) qDebug() << "websocket server listening on port" << port; connect(m_pWebSocketServer, &QWebSocketServer::newConnection, this, &MyWebSocketServer::onNewConnection); connect(m_pWebSocketServer, &QWebSocketServer::closed, this, &MyWebSocketServer::closed); } } //! [constructor] MyWebSocketServer::~MyWebSocketServer() { m_pWebSocketServer->close(); qDeleteAll(m_clients.begin(), m_clients.end()); } //! [onNewConnection] void MyWebSocketServer::onNewConnection() { QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection(); qDebug() << "onNewConnection:"<<pSocket->peerAddress(); connect(pSocket, &QWebSocket::textMessageReceived, this, &MyWebSocketServer::processTextMessage); connect(pSocket, &QWebSocket::binaryMessageReceived, this, &MyWebSocketServer::processBinaryMessage); connect(pSocket, &QWebSocket::disconnected, this, &MyWebSocketServer::socketDisconnected); m_clients << pSocket; } //! [onNewConnection] //! [processTextMessage] void MyWebSocketServer::processTextMessage(QString message) { // QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); // qDebug() <<pClient; // if (m_debug) // qDebug() << "Message received:" << message; // if (pClient) { // qDebug() << "sendTextMessage:" << message; // pClient->sendTextMessage(message); // } if(m_clients.count ()>0){ qDebug() << "sendTextMessage:" << message; m_clients.at(m_clients.count()-1)->sendTextMessage (message); } } //! [processTextMessage] //! [processBinaryMessage] void MyWebSocketServer::processBinaryMessage(QByteArray message) { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); if (m_debug) qDebug() << "Binary Message received:" << message; if (pClient) { pClient->sendBinaryMessage(message); } } //! [processBinaryMessage] //! [socketDisconnected] void MyWebSocketServer::socketDisconnected() { qDebug() << "socketDisconnected:"; QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); if (m_debug) qDebug() << "socketDisconnected:" << pClient; if (pClient) { m_clients.removeAll(pClient); pClient->deleteLater(); } } //! [socketDisconnected]