From b33ad80992011d73559fa3587fd6ec484470e00f Mon Sep 17 00:00:00 2001 From: Fluffy Jenkins Date: Tue, 7 Jan 2020 04:00:13 +0000 Subject: [PATCH] Init push --- .../script-engine/src/WebSocketClass.cpp | 27 ++++++++++++++++++- libraries/script-engine/src/WebSocketClass.h | 1 + 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/libraries/script-engine/src/WebSocketClass.cpp b/libraries/script-engine/src/WebSocketClass.cpp index 56753f07d1..25517b3775 100644 --- a/libraries/script-engine/src/WebSocketClass.cpp +++ b/libraries/script-engine/src/WebSocketClass.cpp @@ -34,10 +34,11 @@ WebSocketClass::WebSocketClass(QScriptEngine* engine, QWebSocket* qWebSocket) : void WebSocketClass::initialize() { connect(_webSocket, &QWebSocket::disconnected, this, &WebSocketClass::handleOnClose); connect(_webSocket, &QWebSocket::textMessageReceived, this, &WebSocketClass::handleOnMessage); + connect(_webSocket, &QWebSocket::binaryMessageReceived, this, &WebSocketClass::handleOnBinaryMessage); connect(_webSocket, &QWebSocket::connected, this, &WebSocketClass::handleOnOpen); connect(_webSocket, static_cast(&QWebSocket::error), this, &WebSocketClass::handleOnError); - _binaryType = QStringLiteral("blob"); + _binaryType = QStringLiteral("arraybuffer"); } QScriptValue WebSocketClass::constructor(QScriptContext* context, QScriptEngine* engine) { @@ -53,8 +54,13 @@ WebSocketClass::~WebSocketClass() { } void WebSocketClass::send(QScriptValue message) { + if (message.isObject()) { + QByteArray ba = qscriptvalue_cast(message); + _webSocket->sendBinaryMessage(ba); + } else { _webSocket->sendTextMessage(message.toString()); } +} void WebSocketClass::close() { this->close(QWebSocketProtocol::CloseCodeNormal); @@ -97,6 +103,25 @@ void WebSocketClass::handleOnMessage(const QString& message) { } } +void WebSocketClass::handleOnBinaryMessage(const QByteArray& message) { + if (_onMessageEvent.isFunction()) { + QScriptValueList args; + QScriptValue arg = _engine->newObject(); + QScriptValue data = _engine->newVariant(QVariant::fromValue(message)); + QScriptValue ctor = _engine->globalObject().property("ArrayBuffer"); + ArrayBufferClass* array = qscriptvalue_cast(ctor.data()); + QScriptValue arrayBuffer; + if (!array) { + qWarning() << "WebSocketClass::handleOnBinaryMessage !ArrayBuffer"; + } else { + arrayBuffer = _engine->newObject(array, data); + } + arg.setProperty("data", arrayBuffer); + args << arg; + _onMessageEvent.call(QScriptValue(), args); + } +} + void WebSocketClass::handleOnOpen() { if (_onOpenEvent.isFunction()) { _onOpenEvent.call(); diff --git a/libraries/script-engine/src/WebSocketClass.h b/libraries/script-engine/src/WebSocketClass.h index dbc9729c61..048ba4bc10 100644 --- a/libraries/script-engine/src/WebSocketClass.h +++ b/libraries/script-engine/src/WebSocketClass.h @@ -123,6 +123,7 @@ private slots: void handleOnClose(); void handleOnError(QAbstractSocket::SocketError error); void handleOnMessage(const QString& message); + void handleOnBinaryMessage(const QByteArray& message); void handleOnOpen(); };