wscenter.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "wscenter.h"
  2. #include <QFile>
  3. #include <QtWebSockets/qwebsocket.h>
  4. #include <QtWebSockets/qwebsocketserver.h>
  5. #include <QtNetwork/QSslCertificate>
  6. #include <QtNetwork/QSslKey>
  7. QT_USE_NAMESPACE
  8. WSCenter::WSCenter(QObject *parent) :
  9. QObject(parent),
  10. m_pWebSocketServer(Q_NULLPTR)
  11. {
  12. m_pWebSocketServer = new QWebSocketServer(QStringLiteral("SSL WebSocket Server"),
  13. QWebSocketServer::NonSecureMode,
  14. // QWebSocketServer::SecureMode,
  15. this);
  16. // QSslConfiguration sslconfiguration;
  17. // QFile certFile(QStringLiteral("/etc/pki/nginx/jd-ioe.com-ca-bundle.crt"));
  18. // QFile keyFile(QStringLiteral("/etc/pki/nginx/jd-ioe.com.key"));
  19. // if(!certFile.open(QIODevice::ReadOnly)){
  20. // fprintf(stderr,"cert file open failed\n");
  21. // exit(1);
  22. // }
  23. // if(!keyFile.open(QIODevice::ReadOnly)){
  24. // fprintf(stderr,"key file open failed\n");
  25. // exit(1);
  26. // }
  27. // QSslCertificate certificate(&certFile, QSsl::Pem);
  28. // QSslKey sslKey(&keyFile, QSsl::Rsa, QSsl::Pem);
  29. // certFile.close();
  30. // keyFile.close();
  31. // sslconfiguration.setPeerVerifyMode(QSslSocket::VerifyNone);
  32. // sslconfiguration.setLocalCertificate(certificate);
  33. // sslconfiguration.setPrivateKey(sslKey);
  34. // sslconfiguration.setProtocol(QSsl::TlsV1SslV3);
  35. // m_pWebSocketServer->setSslConfiguration(sslconfiguration);
  36. connect(m_pWebSocketServer, &QWebSocketServer::newConnection,
  37. this,&WSCenter::onNewConnection);
  38. // connect(m_pWebSocketServer, &QWebSocketServer::sslErrors, this, &WSCenter::onSslErrors);
  39. if(!m_pWebSocketServer->listen(QHostAddress::Any, 55125)){
  40. fprintf(stderr,"WebSocket open failed\n");
  41. exit(1);
  42. }
  43. fprintf(stderr,"WebSocket opened\n");
  44. }
  45. WSCenter::~WSCenter()
  46. {
  47. m_pWebSocketServer->close();
  48. // qDeleteAll(m_clients);
  49. m_clients.clear();
  50. }
  51. void WSCenter::checkLastNote(QString companyCode)
  52. {
  53. QListIterator<WSClient *> so(m_clients);
  54. while(so.hasNext())
  55. {
  56. WSClient *client = so.next();
  57. client->chkLastNote(companyCode);
  58. }
  59. }
  60. void WSCenter::onNewConnection()
  61. {
  62. QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection();
  63. WSClient *client = new WSClient(pSocket, this);
  64. connect(client, &WSClient::closed, this, &WSCenter::socketDisconnected);
  65. m_clients << client;
  66. }
  67. void WSCenter::socketDisconnected()
  68. {
  69. WSClient *pClient = qobject_cast<WSClient *>(sender());
  70. if(pClient){
  71. m_clients.removeAll(pClient);
  72. // pClient->deleteLater();
  73. }
  74. }
  75. void WSCenter::onSslErrors(const QList<QSslError> &)
  76. {
  77. fprintf(stderr,"Ssl errors occurred\n");
  78. }