void WebSocketServer::onNewConnection() { QWebSocket *socket = m_pWebSocketServer->nextPendingConnection(); JsonApi *api = new JsonApi(socket, socket->peerAddress() == QHostAddress::LocalHost); connect(socket, &QWebSocket::disconnected, this, &WebSocketServer::socketDisconnected); connect(socket, &QWebSocket::textMessageReceived, api, &JsonApi::processMessage); m_clients[socket] = api; }
void ReverseHashDServer::onNewConnection(){ QWebSocket *pSocket = m_pReverseHashDServer->nextPendingConnection(); std::cout << "NewConnection " << pSocket->peerAddress().toString().toStdString() << " " << pSocket->peerPort() << "\n"; connect(pSocket, &QWebSocket::textMessageReceived, this, &ReverseHashDServer::processTextMessage); connect(pSocket, &QWebSocket::binaryMessageReceived, this, &ReverseHashDServer::processBinaryMessage); connect(pSocket, &QWebSocket::disconnected, this, &ReverseHashDServer::socketDisconnected); m_clients << pSocket; }
void IqWampRouterPrivate::onNewConnection() { while (m_server->hasPendingConnections()) { QWebSocket *socket = m_server->nextPendingConnection(); #ifdef IQWAMP_DEBUG_MODE qDebug() << "New connection from " << socket->peerAddress().toString(); #endif IqWampCallee *client = new IqWampCallee(socket, this); connect(client, &IqWampCallee::hello, this, &IqWampRouterPrivate::onClientHello); m_clients << client; } }
void CardReader::onSocketDisconnected() { QWebSocket *webSocket = qobject_cast<QWebSocket *>(sender()); if (webSocket) { clients.removeAll(webSocket); frontend_message( QStringLiteral("Client disconnected from %1:%2, now %3 connected client(s) [reason: %4]") .arg(webSocket->peerAddress().toString()) .arg(webSocket->peerPort()) .arg(clients.length()) .arg(webSocket->closeCode()) ); webSocket->deleteLater(); } }
//======================= // PRIVATE SLOTS //======================= //GENERIC SERVER SIGNALS // New Connection Signals void WebServer::NewSocketConnection(){ WebSocket *sock = 0; if(WSServer!=0){ if(WSServer->hasPendingConnections()){ QWebSocket *ws = WSServer->nextPendingConnection(); if( !allowConnection(ws->peerAddress()) ){ ws->close(); } else{ sock = new WebSocket( ws, generateID(), AUTH); } } }else if(TCPServer!=0){ if(TCPServer->hasPendingConnections()){ QSslSocket *ss = TCPServer->nextPendingConnection(); if( !allowConnection(ss->peerAddress()) ){ ss->close(); } else{ sock = new WebSocket( ss, generateID(), AUTH); } } } if(sock==0){ return; } //no new connection //qDebug() << "New Socket Connection"; connect(sock, SIGNAL(SocketClosed(QString)), this, SLOT(SocketClosed(QString)) ); connect(EVENTS, SIGNAL(NewEvent(EventWatcher::EVENT_TYPE, QJsonValue)), sock, SLOT(EventUpdate(EventWatcher::EVENT_TYPE, QJsonValue)) ); OpenSockets << sock; }
void CardReader::onNewConnection() { QWebSocket *webSocket = server->nextPendingConnection(); connect(webSocket, &QWebSocket::disconnected, this, &CardReader::onSocketDisconnected); connect(webSocket, static_cast<void(QWebSocket::*)(QAbstractSocket::SocketError)>(&QWebSocket::error), this, &CardReader::onSocketError); connect(webSocket, &QWebSocket::sslErrors, this, &CardReader::onSslError); clients.append(webSocket); frontend_message(QStringLiteral("Client connected from %1:%2, now %3 connected client(s)").arg(webSocket->peerAddress().toString()).arg(webSocket->peerPort()).arg(clients.length())); }
void WsServer::processTextMessage(QString message) { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); if (!pClient) { return; } qDebug()<<message; QString senderIP = pClient->peerAddress().toString(); QStringList messageParts = message.split(","); if (messageParts[0]=="play") { // comes in as "play,water|stones|sticks|wind|flute,<pan>" QString type = messageParts[1]; double pan = messageParts[2].toDouble(); QString scoreLine; if (type=="flute") { if (onlyOneEventAllowed) { if (!fluteIPs.contains(senderIP)) { fluteIPs.append(senderIP); pClient->sendTextMessage("set flute disable"); emit eventCountChanged(FLUTE, ++eventCounter[FLUTE]); // increase the counter and send signal to UI } else { qDebug()<<"Second try from: " << senderIP; return; } } scoreLine.sprintf("i \"flute\" 0 15 %f ", pan); } else { if (onlyOneEventAllowed) { if (type=="water" && !waterIPs.contains(senderIP)) { waterIPs.append(senderIP); emit eventCountChanged(WATER, ++eventCounter[WATER]); pClient->sendTextMessage("set water disable"); } else if (type=="stones" && !stoneIPs.contains(senderIP)) { stoneIPs.append(senderIP); emit eventCountChanged(STONES, ++eventCounter[STONES]); pClient->sendTextMessage("set stones disable"); } else if (type=="sticks" && !stickIPs.contains(senderIP)) { stickIPs.append(senderIP); emit eventCountChanged(STICKS, ++eventCounter[STICKS]); pClient->sendTextMessage("set sticks disable"); } else if (type=="wind" && !windIPs.contains(senderIP)) { windIPs.append(senderIP); emit eventCountChanged(WIND, ++eventCounter[WIND]); pClient->sendTextMessage("set wind disable"); } else { qDebug()<<"Seems like second try from: " << senderIP; return; } } // get filename as random from the subfolder according to the sound type QString path = "../sounds/"+type + "/"; QDir directory(path); QStringList files = directory.entryList(QStringList()<<"*.wav"); QString filename = path + files[qrand()%files.count()]; //qDebug()<<"File selected: "<<filename; scoreLine.sprintf("i \"play\" 0 5 \"%s\" %f ",filename.toLocal8Bit().data(), pan); } qDebug()<< scoreLine; emit newEvent(scoreLine); } }