radarserver.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "radarserver.h"
  2. RadarServer::RadarServer(QObject *parent, quint16 port) : QObject(parent)
  3. {
  4. this->port = port;
  5. m_pWebSocketServer = new QWebSocketServer(QStringLiteral("Radar Server"),
  6. QWebSocketServer::NonSecureMode,
  7. this);
  8. if (m_pWebSocketServer->listen(QHostAddress::Any, port))
  9. {
  10. qDebug() << "Radar Server listening on port" << port;
  11. connect(m_pWebSocketServer, &QWebSocketServer::newConnection,
  12. this, &RadarServer::onNewConnection);
  13. }
  14. }
  15. RadarServer::~RadarServer()
  16. {
  17. m_pWebSocketServer->close();
  18. qDeleteAll(m_clients.begin(), m_clients.end());
  19. }
  20. void RadarServer::onNewConnection()
  21. {
  22. QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection();
  23. connect(pSocket, &QWebSocket::textMessageReceived, this, &RadarServer::processMessage);
  24. connect(pSocket, &QWebSocket::disconnected, this, &RadarServer::socketDisconnected);
  25. m_clients << pSocket;
  26. }
  27. void RadarServer::processMessage(QString message)
  28. {
  29. bool *ok = new bool();
  30. int order = message.toInt(ok);
  31. if (ok)
  32. {
  33. qDebug() << order;
  34. emit SigRadarGen(order);
  35. }
  36. QWebSocket *pSender = qobject_cast<QWebSocket *>(sender());
  37. pSender->sendTextMessage(QString("111"));
  38. }
  39. void RadarServer::socketDisconnected()
  40. {
  41. QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
  42. if (pClient)
  43. {
  44. m_clients.removeAll(pClient);
  45. pClient->deleteLater();
  46. }
  47. }