Showing the changes to libraries/script-engine/src/WebSocketClass.cpp (1 of 5 files). Show all
Unified · Split
| @@ -16,13 +16,25 @@ · 15 unmodified lines | ||
| 16 | 16 | #include "WebSocketClass.h" |
| 17 | 17 | |
| 18 | 18 | WebSocketClass::WebSocketClass(QScriptEngine* engine, QString url) : |
| 19 | − _engine(engine) | |
| 19 | + _engine(engine), | |
| 20 | + _webSocket(new QWebSocket()) | |
| 20 | 21 | { |
| 21 | − connect(&_webSocket, &QWebSocket::disconnected, this, &WebSocketClass::handleOnClose); | |
| 22 | − connect(&_webSocket, SIGNAL(error()), this, SLOT(handleOnError())); | |
| 23 | − connect(&_webSocket, &QWebSocket::textMessageReceived, this, &WebSocketClass::handleOnMessage); | |
| 24 | − connect(&_webSocket, &QWebSocket::connected, this, &WebSocketClass::handleOnOpen); | |
| 25 | − _webSocket.open(url); | |
| 22 | + initialize(); | |
| 23 | + _webSocket->open(url); | |
| 24 | +} | |
| 25 | + | |
| 26 | +WebSocketClass::WebSocketClass(QScriptEngine* engine, QWebSocket* qWebSocket) : | |
| 27 | + _engine(engine), | |
| 28 | + _webSocket(qWebSocket) | |
| 29 | +{ | |
| 30 | + initialize(); | |
| 31 | +} | |
| 32 | + | |
| 33 | +void WebSocketClass::initialize() { | |
| 34 | + connect(_webSocket, &QWebSocket::disconnected, this, &WebSocketClass::handleOnClose); | |
| 35 | + connect(_webSocket, &QWebSocket::textMessageReceived, this, &WebSocketClass::handleOnMessage); | |
| 36 | + connect(_webSocket, &QWebSocket::connected, this, &WebSocketClass::handleOnOpen); | |
| 37 | + _binaryType = "blob"; | |
| 26 | 38 | } |
| 27 | 39 | |
| 28 | 40 | QScriptValue WebSocketClass::constructor(QScriptContext* context, QScriptEngine* engine) { |
| @@ -39,26 +51,37 @@ WebSocketClass::~WebSocketClass() { · 10 unmodified lines | ||
| 39 | 51 | } |
| 40 | 52 | |
| 41 | 53 | void WebSocketClass::send(QScriptValue message) { |
| 42 | − _webSocket.sendTextMessage(message.toString()); | |
| 54 | + _webSocket->sendTextMessage(message.toString()); | |
| 55 | +} | |
| 56 | + | |
| 57 | +void WebSocketClass::close() { | |
| 58 | + this->close(QWebSocketProtocol::CloseCode::CloseCodeNormal); | |
| 59 | +} | |
| 60 | + | |
| 61 | +void WebSocketClass::close(QWebSocketProtocol::CloseCode closeCode) { | |
| 62 | + this->close(closeCode, QStringLiteral("")); | |
| 43 | 63 | } |
| 44 | 64 | |
| 45 | 65 | void WebSocketClass::close(QWebSocketProtocol::CloseCode closeCode, QString reason) { |
| 46 | − _webSocket.close(closeCode, reason); | |
| 66 | + _webSocket->close(closeCode, reason); | |
| 47 | 67 | } |
| 48 | 68 | |
| 49 | 69 | void WebSocketClass::handleOnClose() { |
| 50 | − if (_onCloseEvent.isFunction()) { | |
| 51 | − //QScriptValueList args; | |
| 52 | − //args << ("received: " + message); | |
| 53 | − _onCloseEvent.call();//(QScriptValue(), args); | |
| 70 | + bool hasError = false; | |
| 71 | + if (_webSocket->error() != QAbstractSocket::SocketError::UnknownSocketError) { | |
| 72 | + hasError = true; | |
| 73 | + if (_onErrorEvent.isFunction()) { | |
| 74 | + _onErrorEvent.call(); | |
| 75 | + } | |
| 54 | 76 | } |
| 55 | −} | |
| 56 | − | |
| 57 | −void WebSocketClass::handleOnError(QAbstractSocket::SocketError error) { | |
| 58 | − if (_onErrorEvent.isFunction()) { | |
| 59 | − // QScriptValueList args; | |
| 60 | − //args << ("received: " + message); | |
| 61 | − _onErrorEvent.call();/// QScriptValue(), args); | |
| 77 | + if (_onCloseEvent.isFunction()) { | |
| 78 | + QScriptValueList args; | |
| 79 | + QScriptValue arg = _engine->newObject(); | |
| 80 | + arg.setProperty("code", hasError ? QWebSocketProtocol::CloseCode::CloseCodeAbnormalDisconnection : _webSocket->closeCode()); | |
| 81 | + arg.setProperty("reason", _webSocket->closeReason()); | |
| 82 | + arg.setProperty("wasClean", !hasError); | |
| 83 | + args << arg; | |
| 84 | + _onCloseEvent.call(QScriptValue(), args); | |
| 62 | 85 | } |
| 63 | 86 | } |
| 64 | 87 | |
| @@ -74,8 +97,22 @@ void WebSocketClass::handleOnMessage(const QString& message) { · 9 unmodified lines | ||
| 74 | 97 | |
| 75 | 98 | void WebSocketClass::handleOnOpen() { |
| 76 | 99 | if (_onOpenEvent.isFunction()) { |
| 77 | − //QScriptValueList args; | |
| 78 | − //args << ("received: " + message); | |
| 79 | − _onOpenEvent.call();// QScriptValue(), args); | |
| 100 | + _onOpenEvent.call(); | |
| 80 | 101 | } |
| 81 | −} | |
| 102 | +} | |
| 103 | + | |
| 104 | +QScriptValue qWSCloseCodeToScriptValue(QScriptEngine* engine, const QWebSocketProtocol::CloseCode &closeCode) { | |
| 105 | + return closeCode; | |
| 106 | +} | |
| 107 | + | |
| 108 | +void qWSCloseCodeFromScriptValue(const QScriptValue &object, QWebSocketProtocol::CloseCode &closeCode) { | |
| 109 | + closeCode = (QWebSocketProtocol::CloseCode)object.toUInt16(); | |
| 110 | +} | |
| 111 | + | |
| 112 | +QScriptValue webSocketToScriptValue(QScriptEngine* engine, WebSocketClass* const &in) { | |
| 113 | + return engine->newQObject(in); | |
| 114 | +} | |
| 115 | + | |
| 116 | +void webSocketFromScriptValue(const QScriptValue &object, WebSocketClass* &out) { | |
| 117 | + out = qobject_cast<WebSocketClass*>(object.toQObject()); | |
| 118 | +} | |