| 19 | |
| 20 | |
| 21 | WebSocketClient::WebSocketClient( |
| 22 | QtHttpRequest* request, QTcpSocket* sock, bool localConnection, QObject* parent) |
| 23 | : QObject(parent) |
| 24 | , _socket(sock) |
| 25 | , _log(Logger::getInstance("WEBSOCKET")) |
| 26 | { |
| 27 | // connect socket; disconnect handled from QtHttpServer |
| 28 | connect(_socket, &QTcpSocket::readyRead, this, &WebSocketClient::handleWebSocketFrame); |
| 29 | |
| 30 | // QtHttpRequest contains all headers for handshake |
| 31 | QByteArray secWebSocketKey = request->getHeader(QtHttpHeader::SecWebSocketKey); |
| 32 | const QString client = request->getClientInfo().clientAddress.toString(); |
| 33 | |
| 34 | // Json processor |
| 35 | _hyperAPI = new HyperAPI(client, _log, localConnection, this); |
| 36 | connect(_hyperAPI, &HyperAPI::SignalCallbackJsonMessage, this, &WebSocketClient::sendMessage); |
| 37 | connect(_hyperAPI, &HyperAPI::SignalCallbackBinaryImageMessage, this, &WebSocketClient::signalCallbackBinaryImageMessageHandler); |
| 38 | connect(_hyperAPI, &HyperAPI::SignalPerformClientDisconnection, this, [this]() { this->sendClose(CLOSECODE::NORMAL); }); |
| 39 | |
| 40 | Debug(_log, "New connection from %s", QSTRING_CSTR(client)); |
| 41 | |
| 42 | // do handshake |
| 43 | secWebSocketKey += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
| 44 | QByteArray hash = QCryptographicHash::hash(secWebSocketKey, QCryptographicHash::Sha1).toBase64(); |
| 45 | |
| 46 | QString data = QString("HTTP/1.1 101 Switching Protocols\r\n") |
| 47 | + QString("Upgrade: websocket\r\n") |
| 48 | + QString("Connection: Upgrade\r\n") |
| 49 | + QString("Sec-WebSocket-Accept: ") + QString(hash.data()) + "\r\n\r\n"; |
| 50 | |
| 51 | _socket->write(QSTRING_CSTR(data), data.size()); |
| 52 | _socket->flush(); |
| 53 | |
| 54 | // Init JsonAPI |
| 55 | _hyperAPI->initialize(); |
| 56 | } |
| 57 | |
| 58 | void WebSocketClient::handleBinaryMessage(QByteArray& data) |
| 59 | { |
nothing calls this directly
no test coverage detected