mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 18:13:05 +02:00
initial WebSocketServer
This commit is contained in:
parent
1664792e00
commit
7c77e0e941
4 changed files with 87 additions and 1 deletions
|
@ -39,6 +39,7 @@
|
||||||
#include "TypedArrays.h"
|
#include "TypedArrays.h"
|
||||||
#include "XMLHttpRequestClass.h"
|
#include "XMLHttpRequestClass.h"
|
||||||
#include "WebSocketClass.h"
|
#include "WebSocketClass.h"
|
||||||
|
#include "WebSocketServerClass.h"
|
||||||
|
|
||||||
#include "SceneScriptingInterface.h"
|
#include "SceneScriptingInterface.h"
|
||||||
|
|
||||||
|
@ -347,6 +348,9 @@ void ScriptEngine::init() {
|
||||||
QScriptValue webSocketConstructorValue = newFunction(WebSocketClass::constructor);
|
QScriptValue webSocketConstructorValue = newFunction(WebSocketClass::constructor);
|
||||||
globalObject().setProperty("WebSocket", webSocketConstructorValue);
|
globalObject().setProperty("WebSocket", webSocketConstructorValue);
|
||||||
|
|
||||||
|
QScriptValue webSocketServerConstructorValue = newFunction(WebSocketServerClass::constructor);
|
||||||
|
globalObject().setProperty("WebSocketServer", webSocketServerConstructorValue);
|
||||||
|
|
||||||
QScriptValue printConstructorValue = newFunction(debugPrint);
|
QScriptValue printConstructorValue = newFunction(debugPrint);
|
||||||
globalObject().setProperty("print", printConstructorValue);
|
globalObject().setProperty("print", printConstructorValue);
|
||||||
|
|
||||||
|
|
|
@ -64,7 +64,6 @@ void WebSocketClass::handleOnError(QAbstractSocket::SocketError error) {
|
||||||
|
|
||||||
void WebSocketClass::handleOnMessage(const QString& message) {
|
void WebSocketClass::handleOnMessage(const QString& message) {
|
||||||
if (_onMessageEvent.isFunction()) {
|
if (_onMessageEvent.isFunction()) {
|
||||||
|
|
||||||
QScriptValueList args;
|
QScriptValueList args;
|
||||||
QScriptValue arg = _engine->newObject();
|
QScriptValue arg = _engine->newObject();
|
||||||
arg.setProperty("data", message);
|
arg.setProperty("data", message);
|
||||||
|
|
44
libraries/script-engine/src/WebSocketServerClass.cpp
Normal file
44
libraries/script-engine/src/WebSocketServerClass.cpp
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
//
|
||||||
|
// WebSocketServerClass.cpp
|
||||||
|
// libraries/script-engine/src/
|
||||||
|
//
|
||||||
|
// Created by Thijs Wenker on 8/10/15.
|
||||||
|
// Copyright (c) 2015 High Fidelity, Inc. All rights reserved.
|
||||||
|
//
|
||||||
|
// Making WebSocketServer accessible through scripting.
|
||||||
|
//
|
||||||
|
// Distributed under the Apache License, Version 2.0.
|
||||||
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "ScriptEngine.h"
|
||||||
|
#include "WebSocketServerClass.h"
|
||||||
|
|
||||||
|
WebSocketServerClass::WebSocketServerClass(QScriptEngine* engine, const QString& serverName, quint16 port) :
|
||||||
|
_engine(engine),
|
||||||
|
_webSocketServer(serverName, QWebSocketServer::SslMode::NonSecureMode)
|
||||||
|
{
|
||||||
|
_webSocketServer.listen(QHostAddress::Any, port);
|
||||||
|
}
|
||||||
|
|
||||||
|
QScriptValue WebSocketServerClass::constructor(QScriptContext* context, QScriptEngine* engine) {
|
||||||
|
// the serverName is used in handshakes
|
||||||
|
QString serverName = QStringLiteral("HighFidelity - Scripted WebSocket Listener");
|
||||||
|
// port 0 will auto-assign a free port
|
||||||
|
quint16 port = 0;
|
||||||
|
QScriptValue callee = context->callee();
|
||||||
|
if (context->argumentCount() > 0) {
|
||||||
|
QScriptValue options = context->argument(0);
|
||||||
|
QScriptValue portOption = options.property(QStringLiteral("port"));
|
||||||
|
if (portOption.isValid() && portOption.isNumber()) {
|
||||||
|
port = portOption.toNumber();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return engine->newQObject(new WebSocketServerClass(engine, serverName, port));
|
||||||
|
}
|
||||||
|
|
||||||
|
WebSocketServerClass::~WebSocketServerClass() {
|
||||||
|
if (_webSocketServer.isListening()) {
|
||||||
|
_webSocketServer.close();
|
||||||
|
}
|
||||||
|
}
|
39
libraries/script-engine/src/WebSocketServerClass.h
Normal file
39
libraries/script-engine/src/WebSocketServerClass.h
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
//
|
||||||
|
// WebSocketServerClass.h
|
||||||
|
// libraries/script-engine/src/
|
||||||
|
//
|
||||||
|
// Created by Thijs Wenker on 8/10/15.
|
||||||
|
// Copyright (c) 2015 High Fidelity, Inc. All rights reserved.
|
||||||
|
//
|
||||||
|
// Distributed under the Apache License, Version 2.0.
|
||||||
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef hifi_WebSocketServerClass_h
|
||||||
|
#define hifi_WebSocketServerClass_h
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include <QScriptEngine>
|
||||||
|
#include <QWebSocketServer>
|
||||||
|
|
||||||
|
class WebSocketServerClass : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
WebSocketServerClass(QScriptEngine* engine, const QString& serverName, quint16 port);
|
||||||
|
~WebSocketServerClass();
|
||||||
|
|
||||||
|
static QScriptValue WebSocketServerClass::constructor(QScriptContext* context, QScriptEngine* engine);
|
||||||
|
|
||||||
|
private:
|
||||||
|
QWebSocketServer _webSocketServer;
|
||||||
|
QScriptEngine* _engine;
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void newConnection();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // hifi_WebSocketServerClass_h
|
Loading…
Reference in a new issue