void wsServer::processBinaryMessage(QByteArray message) { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); if (pClient) { pClient->sendBinaryMessage(message); } }
void onDisconnected() { JOYNR_LOG_TRACE(logger, "on disconnected"); QWebSocket* client = qobject_cast<QWebSocket*>(sender()); if(client) { client->deleteLater(); } }
// remove the client from when it disconneced void ServerDeamon::socketDisconnected() { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); if (pClient) { m_clients.removeAll(pClient); pClient->deleteLater(); } }
void ShtServer::onNewConnection() { QWebSocket *socket = socketServer->nextPendingConnection(); connect(socket, &QWebSocket::textMessageReceived, this, &ShtServer::onTextMessage); connect(socket, &QWebSocket::disconnected, this, &ShtServer::onSocketDisconnected); //onlineSockets << socket; qWarning() << "Added socket " << socket->peerName() << ":" << socket->peerPort(); }
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; }
//! [processTextMessage] void SslEchoServer::processTextMessage(QString message) { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); if (pClient) { pClient->sendTextMessage(message); } }
void ReverseHashDServer::socketDisconnected() { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); std::cout << "socketDisconnected:" << pClient << "\n"; if (pClient) { m_clients.removeAll(pClient); pClient->deleteLater(); } }
void WsServer::socketDisconnected() { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); if (pClient) { m_clients.removeAll(pClient); emit newConnection(m_clients.count()); pClient->deleteLater(); } }
void EchoServer::processTextMessage(QString message) { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); //if (m_debug) qDebug() << "Message received:" << message; if (pClient) { pClient->sendTextMessage(QString("服务器收到了") + message); } }
//! [processTextMessage] void BCWebSocketServer::processTextMessage(QString message) { qDebug() << message ; QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); if (pClient) { pClient->sendTextMessage(message); } }
void Server::processBinaryMessage(QByteArray message) { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); if (m_debug) qDebug() << "Binary Message received:" << message; if (pClient) { pClient->sendBinaryMessage(message); } }
//! [socketDisconnected] void SslEchoServer::socketDisconnected() { qDebug() << "Client disconnected"; QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); if (pClient) { m_clients.removeAll(pClient); pClient->deleteLater(); } }
void Server::socketDisconnected() { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); if (m_debug) qDebug() << "socketDisconnected:" << pClient; if (pClient) { m_clients.removeAll(pClient); pClient->deleteLater(); } }
// --------------------------------------------------------------------- WsCln::~WsCln() { QWebSocket* socket; foreach (socket, servers) { #ifdef QT53 socket->close(); #else socket->disconnectFromHost(); #endif }
// _____________________________________________________________________ void CWebSocketServer::socketDisconnected() { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); if (pClient) { int id = m_clients[pClient]; m_clients.remove(pClient); m_clients_rev.remove(id); pClient->deleteLater(); } }
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 OpenGLServer::onNewConnection() { QWebSocket *pSocket = mpWebSocketServer->nextPendingConnection(); connect(pSocket, &QWebSocket::binaryMessageReceived, this, &OpenGLServer::processBinaryMessage); connect(pSocket, &QWebSocket::disconnected, this, &OpenGLServer::socketDisconnected); connect(&trace::localWriter, &trace::LocalWriter::frameEnd, this, &OpenGLServer::onFrameEnd); mClients << qMakePair(pSocket, false); pSocket->sendBinaryMessage(trace::localWriter.getInitFrame()); }
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; } }
//! [onNewConnection] void SslEchoServer::onNewConnection() { QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection(); qDebug() << "Client connected:" << pSocket->peerName() << pSocket->origin(); connect(pSocket, SIGNAL(textMessageReceived(QString)), this, SLOT(processMessage(QString))); connect(pSocket, SIGNAL(binaryMessageReceived(QByteArray)), this, SLOT(processBinaryMessage(QByteArray))); connect(pSocket, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); //connect(pSocket, SIGNAL(pong(quint64)), this, SLOT(processPong(quint64))); m_clients << pSocket; }
// _____________________________________________________________________ void CWebSocketServer::write(int client_id, QByteArray data) { //qDebug() << "CWebSocketServer::write" << client_id << data; QWebSocket *pClient = NULL; if (m_clients_rev.contains(client_id)) { pClient = m_clients_rev[client_id]; } if (pClient) { if (m_transmission_mode == BINARY_MODE) pClient->sendBinaryMessage(data); else pClient->sendTextMessage(data); emit bytesWritten(client_id, data.size()); } }
//! [onNewConnection] void SslEchoServer::onNewConnection() { QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection(); qDebug() << "Client connected:" << pSocket->peerName() << pSocket->origin(); connect(pSocket, &QWebSocket::textMessageReceived, this, &SslEchoServer::processTextMessage); connect(pSocket, &QWebSocket::binaryMessageReceived, this, &SslEchoServer::processBinaryMessage); connect(pSocket, &QWebSocket::disconnected, this, &SslEchoServer::socketDisconnected); //connect(pSocket, &QWebSocket::pong, this, &SslEchoServer::processPong); m_clients << pSocket; }
void OpenGLServer::socketDisconnected() { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); if (mDebug) qDebug() << "socketDisconnected:" << pClient; if (pClient) { Clients::iterator it = std::find_if(mClients.begin(), mClients.end(), [pClient](const ClientEntry &clientEntry) { return clientEntry.first == pClient; }); if (it != mClients.end() ) { mClients.removeAll(*it); } pClient->deleteLater(); } }
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(); } }
/*! \internal */ void QWebSocketServerPrivate::close(bool aboutToDestroy) { Q_Q(QWebSocketServer); m_pTcpServer->close(); while (!m_pendingConnections.isEmpty()) { QWebSocket *pWebSocket = m_pendingConnections.dequeue(); pWebSocket->close(QWebSocketProtocol::CloseCodeGoingAway, QWebSocketServer::tr("Server closed.")); pWebSocket->deleteLater(); } if (!aboutToDestroy) { //emit signal via the event queue, so the server gets time //to process any hanging events, like flushing buffers aso QMetaObject::invokeMethod(q, "closed", Qt::QueuedConnection); } }
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())); }
//======================= // 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 Server::processTextMessage(QString message) { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); QSqlQuery query("SELECT players.first_name, players.last_name, players.date_of_birth FROM players LIMIT 0,10" , db.myDatabase); /* * Pseudo code (frontend): * recieve JSon object * present JSon object to the user * * TODO: * * Frontend: * Get a input more usable than a string, * maybe through a table (generated by a hardcoded query) made clickable through JQuery * * Backend: * If the Pseudo code above is complete it should be pretty much done. * Pretty up the code, fix the Databasemanager class, it is pretty useless atm. */ QJsonObject myJsonObject; QString tArray[3] = {"players.first_name", "players.last_name", "players.date_of_birth "}; QJsonArray myArray[3]; while(query.next()) for(int i=0 ; i<3 ; i++) { myArray[i].push_back(query.value(i).toString()); } for(int i = 0 ; i<3 ; i++) myJsonObject.insert(tArray[i], myArray[i]); QJsonDocument myDoc(myJsonObject); if (pClient) { qDebug() << myDoc.toJson(); pClient->sendTextMessage(myDoc.toJson()); } }
/*! Called from QWebSocketServer \internal */ QWebSocket *QWebSocketPrivate::upgradeFrom(QTcpSocket *pTcpSocket, const QWebSocketHandshakeRequest &request, const QWebSocketHandshakeResponse &response, QObject *parent) { QWebSocket *pWebSocket = new QWebSocket(pTcpSocket, response.acceptedVersion(), parent); if (Q_LIKELY(pWebSocket)) { pWebSocket->d_func()->setExtension(response.acceptedExtension()); pWebSocket->d_func()->setOrigin(request.origin()); pWebSocket->d_func()->setRequestUrl(request.requestUrl()); pWebSocket->d_func()->setProtocol(response.acceptedProtocol()); pWebSocket->d_func()->setResourceName(request.requestUrl().toString(QUrl::RemoveUserInfo)); //a server should not send masked frames pWebSocket->d_func()->enableMasking(false); } return pWebSocket; }
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); } }
void ShtServer::onTextMessage(QString msg) { QElapsedTimer totalTime; totalTime.start(); QWebSocket *socket = qobject_cast<QWebSocket *>(sender()); qWarning() << "received: " << msg << "from " << socket->peerName(); //Json QJsonDocument doc = QJsonDocument::fromJson(msg.toUtf8()); if(!doc.isObject()){ socket->sendTextMessage("json or gtfo"); return; } QJsonObject docObj = doc.object(); int r = docObj["request"].toInt(); switch(r){ case 0: //LOGIN { QString uuid = docObj["uuid"].toString(); User* u = dbManager->findUserByUuid(uuid); if(!u){ //DEFAULTS FOR NEW USERS u = new User(); u->setUuid(QUuid::createUuid().toString()); u->setName("anonimo"); u->setEmail(""); u->setMatches(0); u->setKilos(0); u->setState(User::AVAILABLE); u->setAvatar("263a"); dbManager->addUser(u); u->setSocket(socket); } usersBySocket.insert(socket, u); QJsonObject obj = u->getJsonObject(); QJsonDocument doc; doc.setObject(obj); QByteArray msg = doc.toJson(QJsonDocument::Compact); socket->sendTextMessage(msg); }break; case 1: //GET MATCHES { User *u = usersBySocket.value(socket); if(!u){ qWarning() << "WARNING: getMatches from unknown user"; break; } QJsonArray matchesList; for(Match* m: matches){ QJsonObject matchObj = m->getJsonObject(); matchesList.append(matchObj); } QJsonObject obj; obj["request"] = 1; obj["matchesList"] = matchesList; QJsonDocument doc; doc.setObject(obj); QByteArray msg = doc.toJson(QJsonDocument::Compact); socket->sendTextMessage(msg); }break; case 2: //CREATE MATCH { User* u = usersBySocket.value(socket); if(!u){ qWarning() << "ERROR: createMatch() user not found"; break; } if(u->getPendingMatch() != NULL){ qWarning() << "SKIPPING: user already has a match pending"; break; } Match *m = new Match(this); m->init(u); m->addPlayer(u); matches.append(m); u->setPendingMatch(m); QJsonObject obj; obj["request"] = 2; obj["result"] = "ok"; QJsonDocument doc; doc.setObject(obj); QByteArray msg = doc.toJson(QJsonDocument::Compact); socket->sendTextMessage(msg); connect(m, &Match::finished, this, &ShtServer::onMatchFinished); }break; case 3: //JOIN MATCH { User* u = usersBySocket.value(socket); if(!u){ qWarning() << "ERROR: joinMatch() user not found"; break; } Match* m = findMatch(docObj["uuid"].toString()); if(!m){ qWarning() << "ERROR: match not found"; break; } m->addPlayer(u); u->setPendingMatch(m); }break; case 4: //LEAVE MATCH { User* u = usersBySocket.value(socket); if(!u){ qWarning() << "ERROR: leaveMatch() user not found"; break; } Match* m = findMatch(docObj["uuid"].toString()); if(!m){ qWarning() << "ERROR: match not found"; break; } m->leavePlayer(u); }break; case 5: //PASS CARD { User* u = usersBySocket.value(socket); if(!u){ qWarning() << "ERROR: passCard user not found"; break; } Match* m = findMatch(docObj["uuid"].toString()); if(!m){ qWarning() << "ERROR: match not found"; break; } m->passCard(u, docObj["card"].toString()); }break; case 6: //START MATCH { User* u = usersBySocket.value(socket); if(!u){ qWarning() << "ERROR: matchStart user not found"; break; } Match* m = findMatch(docObj["uuid"].toString()); if(!m){ qWarning() << "ERROR: match not found"; break; } //Only match creator can start the match if(m->state()==Match::INIT && m->getOwner() == u){ m->start(); } else { u->sendMessage("match_server", "start_game", "denied", ""); } }break; default: break; } qWarning() << "Request " << r << " evaded in " << totalTime.elapsed() << "ms"; }