MainWindow::MainWindow(bool &ok, QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), _webSocket(NULL), _model(NULL) { ui->setupUi(this); SidInfo *info = new SidInfo; ui->columnView->setPreviewWidget(info); QSettings settings; QString url = settings.value("url").toString(); if (url.isEmpty()) { url = runConnectionDialog(); if (url.isEmpty()) { ok = false; return; } wsConnect(url); } else { wsConnect(url); } connect(ui->labelCurrentSong, &QLabel::linkActivated, this, &MainWindow::onNavigation); ok = true; }
bool HttpServer::initWebSocket(HttpServerConnection& connection, HttpRequest& request, HttpResponse& response) { if (!wsEnabled) return false; WebSocket *sock = new WebSocket(&connection); if (!sock->initialize(request, response)) return false; connection.setTimeOut(USHRT_MAX); //Disable disconnection on connection idle (no rx/tx) connection.setDisconnectionHandler(HttpServerConnectionDelegate(&HttpServer::onCloseWebSocket, this)); // auto remove on close response.sendHeader(connection); // Will push header before user data wsocks.addElement(sock); if (wsConnect) wsConnect(*sock); if (wsCommandEnabled && (request.getQueryParameter(wsCommandRequestParam) == "true")) { #if ENABLE_CMD_EXECUTOR debugf("WebSocket Commandprocessor started"); #else debugf("WebSocket Commandprocessor support DISABLED via ENABLE_CMD_EXECUTOR"); #endif sock->enableCommand(); } }
void MainWindow::onDisconnected() { qDebug() << "WebSocket disconnected"; _webSocket->close(); QString url = runConnectionDialog(); if (url.isEmpty()) { qApp->exit(1); } wsConnect(url); }
bool HttpServer::initWebSocket(HttpServerConnection& connection, HttpRequest& request, HttpResponse& response) { if (!wsEnabled) return false; auto sock = WebSocket(&connection); if (!sock.initialize(request, response)) return false; connection.setDisconnectionHandler(HttpServerConnectionDelegate(&HttpServer::onCloseWebSocket, this)); // auto remove on close response.sendHeader(connection); // Will push header before user data wsocks.add(sock); if (wsConnect) wsConnect(sock); return true; }
err_t WebsocketClient::onReceive(pbuf* buf) { if (buf == NULL) { // Disconnected, close it TcpClient::onReceive(buf); } else { uint16_t size = buf->tot_len; uint8_t* data = new uint8_t[size]; pbuf_copy_partial(buf, data, size, 0); switch (_mode) { case wsMode::Connecting: if (_verifyKey((char*)data, size) == true) { _mode = wsMode::Connected; // debugf("Key Verified. Websocket Handshake completed"); sendPing(); } else { _mode = wsMode::Disconnected; // Handshake was not proper. } if (wsConnect) { wsConnect(*this, _mode); } break; case wsMode::Connected: WebsocketFrameClass wsFrame; do { if (wsFrame.decodeFrame(data, size)) { switch (wsFrame._frameType) { case WSFrameType::text: { // debugf("Got text frame"); String msg; msg.setString((char*)wsFrame._payload, wsFrame._payloadLength); if (wsMessage) { wsMessage(*this, msg.c_str()); } break; } case WSFrameType::binary: { // debugf("Got binary frame"); if (wsBinary) { wsBinary(*this, wsFrame._payload, wsFrame._payloadLength); } break; } case WSFrameType::close: { debugf("Got Disconnect request from server.\n"); //RFC requires we return a close op code before closing the connection disconnect(); break; } case WSFrameType::ping: { debugf("Got ping ..."); sendPong(); //Need to send Pong in response to Ping break; } case WSFrameType::pong: { debugf("Got pong ..."); //A pong can contain app data, but shouldnt if we didnt send any... break; } case WSFrameType::error: { debugf("ERROR parsing frame!"); break; } case WSFrameType::incomplete: { debugf("INCOMPLETE websocket frame!"); break; } default: { debugf("Unknown frameType: %d", wsFrame._frameType); break; } } } } while (wsFrame._nextReadOffset > 0); break; } delete[] data; TcpClient::onReceive(buf); } }