1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- #include "radarserver.h"
- RadarServer::RadarServer(QObject *parent, quint16 port) : QObject(parent)
- {
- this->port = port;
- m_pWebSocketServer = new QWebSocketServer(QStringLiteral("Radar Server"),
- QWebSocketServer::NonSecureMode,
- this);
- if (m_pWebSocketServer->listen(QHostAddress::Any, port))
- {
- qDebug() << "Radar Server listening on port" << port;
- connect(m_pWebSocketServer, &QWebSocketServer::newConnection,
- this, &RadarServer::onNewConnection);
- }
- }
- RadarServer::~RadarServer()
- {
- m_pWebSocketServer->close();
- qDeleteAll(m_clients.begin(), m_clients.end());
- }
- void RadarServer::onNewConnection()
- {
- QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection();
- connect(pSocket, &QWebSocket::textMessageReceived, this, &RadarServer::processMessage);
- connect(pSocket, &QWebSocket::disconnected, this, &RadarServer::socketDisconnected);
- m_clients << pSocket;
- }
- void RadarServer::processMessage(QString message)
- {
- bool *ok = new bool();
- int order = message.toInt(ok);
- if (ok)
- {
- qDebug() << order;
- emit SigRadarGen(order);
- }
- QWebSocket *pSender = qobject_cast<QWebSocket *>(sender());
- pSender->sendTextMessage(QString("111"));
- }
- void RadarServer::socketDisconnected()
- {
- QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
- if (pClient)
- {
- m_clients.removeAll(pClient);
- pClient->deleteLater();
- }
- }
|