Overte C++ Documentation
WebSocketServerClass.h
1 //
2 // WebSocketServerClass.h
3 // libraries/script-engine/src/
4 //
5 // Created by Thijs Wenker on 8/10/15.
6 // Copyright (c) 2015 High Fidelity, Inc. All rights reserved.
7 // Copyright 2023 Overte e.V.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 // SPDX-License-Identifier: Apache-2.0
12 //
13 
16 
17 #ifndef hifi_WebSocketServerClass_h
18 #define hifi_WebSocketServerClass_h
19 
20 #include <QObject>
21 #include <QWebSocketServer>
22 #include "WebSocketClass.h"
23 
24 #include "ScriptValue.h"
25 
26 class ScriptContext;
27 class ScriptEngine;
28 
29 /*@jsdoc
30  * Manages {@link WebSocket}s in server entity and assignment client scripts.
31  *
32  * <p>Create using <code>new WebSocketServer(...)</code>.</p>
33  *
34  * @class WebSocketServer
35  *
36  * @hifi-server-entity
37  * @hifi-assignment-client
38  *
39  * @property {string} url - The URL that the server is listening on. <em>Read-only.</em>
40  * @property {number} port - The port that the server is listening on. <em>Read-only.</em>
41  * @property {boolean} listening - <code>true</code> if the server is listening for incoming connections, <code>false</code> if
42  * it isn't. <em>Read-only.</em>
43  *
44  * @example <caption>Echo a message back to sender.</caption>
45  * // Server entity script. Echoes received message back to sender.
46  * (function () {
47  * print("Create WebSocketServer");
48  * var webSocketServer = new WebSocketServer();
49  * print("Server url:", webSocketServer.url);
50  *
51  * function onNewConnection(webSocket) {
52  * print("New connection");
53  *
54  * webSocket.onmessage = function (message) {
55  * print("Message received:", message.data);
56  *
57  * var returnMessage = message.data + " back!";
58  * print("Echo a message back:", returnMessage);
59  * webSocket.send(message.data + " back!");
60  * };
61  * }
62  *
63  * webSocketServer.newConnection.connect(onNewConnection);
64  * })
65  *
66  * @example
67  * // Interface script. Bounces message off server entity script.
68  * // Use the server URL reported by the server entity script.
69  * var WEBSOCKET_PING_URL = "ws://127.0.0.1:nnnnn";
70  * var TEST_MESSAGE = "Hello";
71  *
72  * print("Create WebSocket");
73  * var webSocket = new WebSocket(WEBSOCKET_PING_URL);
74  *
75  * webSocket.onmessage = function(data) {
76  * print("Message received:", data.data);
77  * };
78  *
79  * webSocket.onopen = function() {
80  * print("WebSocket opened");
81  * print("Send test message:", TEST_MESSAGE);
82  * webSocket.send(TEST_MESSAGE);
83  * };
84  */
86 class WebSocketServerClass : public QObject {
87  Q_OBJECT
88  Q_PROPERTY(QString url READ getURL)
89  Q_PROPERTY(quint16 port READ getPort)
90  Q_PROPERTY(bool listening READ isListening)
91 
92 public:
93  WebSocketServerClass(ScriptEngine* engine, const QString& serverName, const quint16 port);
95 
96  QString getURL() { return _webSocketServer.serverUrl().toDisplayString(); }
97  quint16 getPort() { return _webSocketServer.serverPort(); }
98  bool isListening() { return _webSocketServer.isListening(); }
99 
100  static ScriptValue constructor(ScriptContext* context, ScriptEngine* engine);
101 
102 public slots:
103 
104  /*@jsdoc
105  * Closes all connections and closes the WebSocketServer.
106  * @function WebSocketServer.close
107  */
108  void close();
109 
110 private:
111  QWebSocketServer _webSocketServer;
112  ScriptEngine* _engine;
113  QList<WebSocketClass*> _clients;
114 
115 private slots:
116  void onNewConnection();
117 
118 signals:
119 
120  /*@jsdoc
121  * Triggered when there is a new connection.
122  * @function WebSocketServer.newConnection
123  * @param {WebSocket} webSocket - The {@link WebSocket} for the new connection.
124  * @returns {Signal}
125  */
126  void newConnection(WebSocketClass* client);
127 
128 };
129 
130 #endif // hifi_WebSocketServerClass_h
131 
[ScriptInterface] Provides an engine-independent interface for QScriptContext
Definition: ScriptContext.h:55
Provides an engine-independent interface for a scripting engine.
Definition: ScriptEngine.h:93
[ScriptInterface] Provides an engine-independent interface for QScriptValue
Definition: ScriptValue.h:40
Provides the WebSocket scripting interface.
Definition: WebSocketClass.h:91
Provides the WebSocketServer scripting interface.
Definition: WebSocketServerClass.h:86