client.cpp 1006 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "client.h"
  2. client::client(QObject *parent, QUrl url) : QObject(parent)
  3. {
  4. qDebug() << "WebSocket server:" << url;
  5. m_url = url;
  6. connect(&m_webSocket, &QWebSocket::connected, this, &client::onConnected);
  7. connect(&m_webSocket, &QWebSocket::disconnected, this, &client::closed);
  8. m_webSocket.open(m_url);
  9. }
  10. void client::SlotSendStyleToSim(QString newstyle)
  11. {
  12. msg = newstyle;
  13. m_webSocket.sendTextMessage(msg);
  14. }
  15. void client::onConnected()
  16. {
  17. qDebug() << "WebSocket connected";
  18. connect(&m_webSocket, &QWebSocket::textMessageReceived,
  19. this, &client::onTextMessageReceived);
  20. // m_webSocket.sendTextMessage(msg);
  21. }
  22. void client::onTextMessageReceived(QString message)
  23. {
  24. qDebug() << "Message received:" << message;
  25. bool *ok = new bool();
  26. int msg = message.toInt(ok);
  27. if (ok)
  28. {
  29. if (msg == 111)
  30. {
  31. qDebug()<<QString::fromLocal8Bit("收到应答");
  32. }
  33. }
  34. delete ok;
  35. // m_webSocket.close();
  36. }