#include "wscenter.h" #include #include #include #include #include QT_USE_NAMESPACE WSCenter::WSCenter(QObject *parent) : QObject(parent), m_pWebSocketServer(Q_NULLPTR) { m_pWebSocketServer = new QWebSocketServer(QStringLiteral("SSL WebSocket Server"), QWebSocketServer::NonSecureMode, // QWebSocketServer::SecureMode, this); // QSslConfiguration sslconfiguration; // QFile certFile(QStringLiteral("/etc/pki/nginx/jd-ioe.com-ca-bundle.crt")); // QFile keyFile(QStringLiteral("/etc/pki/nginx/jd-ioe.com.key")); // if(!certFile.open(QIODevice::ReadOnly)){ // fprintf(stderr,"cert file open failed\n"); // exit(1); // } // if(!keyFile.open(QIODevice::ReadOnly)){ // fprintf(stderr,"key file open failed\n"); // exit(1); // } // QSslCertificate certificate(&certFile, QSsl::Pem); // QSslKey sslKey(&keyFile, QSsl::Rsa, QSsl::Pem); // certFile.close(); // keyFile.close(); // sslconfiguration.setPeerVerifyMode(QSslSocket::VerifyNone); // sslconfiguration.setLocalCertificate(certificate); // sslconfiguration.setPrivateKey(sslKey); // sslconfiguration.setProtocol(QSsl::TlsV1SslV3); // m_pWebSocketServer->setSslConfiguration(sslconfiguration); connect(m_pWebSocketServer, &QWebSocketServer::newConnection, this,&WSCenter::onNewConnection); // connect(m_pWebSocketServer, &QWebSocketServer::sslErrors, this, &WSCenter::onSslErrors); if(!m_pWebSocketServer->listen(QHostAddress::Any, 55125)){ fprintf(stderr,"WebSocket open failed\n"); exit(1); } fprintf(stderr,"WebSocket opened\n"); } WSCenter::~WSCenter() { m_pWebSocketServer->close(); // qDeleteAll(m_clients); m_clients.clear(); } void WSCenter::checkLastNote(QString companyCode) { QListIterator so(m_clients); while(so.hasNext()) { WSClient *client = so.next(); client->chkLastNote(companyCode); } } void WSCenter::onNewConnection() { QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection(); WSClient *client = new WSClient(pSocket, this); connect(client, &WSClient::closed, this, &WSCenter::socketDisconnected); m_clients << client; } void WSCenter::socketDisconnected() { WSClient *pClient = qobject_cast(sender()); if(pClient){ m_clients.removeAll(pClient); // pClient->deleteLater(); } } void WSCenter::onSslErrors(const QList &) { fprintf(stderr,"Ssl errors occurred\n"); }