History

added basic implementation of WebSocket class ( https://developer.mozilla.org/en-US/docs/Web/API/WebSocket ) using QWebSocket

Thijs Wenker committed 11 years ago

Commit 25266b91 parent 597316b5 files · +206 −2Browse files

Showing the changes to libraries/script-engine/src/WebSocketClass.cpp (1 of 5 files). Show all

Unified · Split

libraries/script-engine/src/WebSocketClass.cpp+82−0
@@ -0,0 +1,82 @@
1+//
2+// WebSocketClass.cpp
3+// libraries/script-engine/src/
4+//
5+// Created by Thijs Wenker on 8/4/15.
6+// Copyright (c) 2015 High Fidelity, Inc. All rights reserved.
7+//
8+// This class is an implementation of the WebSocket object for scripting use. It provides a near-complete implementation
9+// of the class described in the Mozilla docs: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
10+//
11+// Distributed under the Apache License, Version 2.0.
12+// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
13+//
14+
15+#include "ScriptEngine.h"
16+#include "WebSocketClass.h"
17+
18+WebSocketClass::WebSocketClass(QScriptEngine* engine, QString url) :
19+ _engine(engine)
20+{
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);
26+}
27+
28+QScriptValue WebSocketClass::constructor(QScriptContext* context, QScriptEngine* engine) {
29+ QString url;
30+ QScriptValue callee = context->callee();
31+ if (context->argumentCount() == 1) {
32+ url = context->argument(0).toString();
33+ }
34+ return engine->newQObject(new WebSocketClass(engine, url));
35+}
36+
37+WebSocketClass::~WebSocketClass() {
38+
39+}
40+
41+void WebSocketClass::send(QScriptValue message) {
42+ _webSocket.sendTextMessage(message.toString());
43+}
44+
45+void WebSocketClass::close(QWebSocketProtocol::CloseCode closeCode, QString reason) {
46+ _webSocket.close(closeCode, reason);
47+}
48+
49+void WebSocketClass::handleOnClose() {
50+ if (_onCloseEvent.isFunction()) {
51+ //QScriptValueList args;
52+ //args << ("received: " + message);
53+ _onCloseEvent.call();//(QScriptValue(), args);
54+ }
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);
62+ }
63+}
64+
65+void WebSocketClass::handleOnMessage(const QString& message) {
66+ if (_onMessageEvent.isFunction()) {
67+
68+ QScriptValueList args;
69+ QScriptValue arg = _engine->newObject();
70+ arg.setProperty("data", message);
71+ args << arg;
72+ _onMessageEvent.call(QScriptValue(), args);
73+ }
74+}
75+
76+void WebSocketClass::handleOnOpen() {
77+ if (_onOpenEvent.isFunction()) {
78+ //QScriptValueList args;
79+ //args << ("received: " + message);
80+ _onOpenEvent.call();// QScriptValue(), args);
81+ }
82+}