History

initial WebSocketServer

Thijs Wenker committed 11 years ago

Commit 7c77e0e1 parent 16647924 files · +87 −1Browse files

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

Unified · Split

libraries/script-engine/src/WebSocketServerClass.cpp+44−0
@@ -0,0 +1,44 @@
1//
2// WebSocketServerClass.cpp
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//
8// Making WebSocketServer accessible through scripting.
9//
10// Distributed under the Apache License, Version 2.0.
11// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
12//
13
14#include "ScriptEngine.h"
15#include "WebSocketServerClass.h"
16
17WebSocketServerClass::WebSocketServerClass(QScriptEngine* engine, const QString& serverName, quint16 port) :
18 _engine(engine),
19 _webSocketServer(serverName, QWebSocketServer::SslMode::NonSecureMode)
20{
21 _webSocketServer.listen(QHostAddress::Any, port);
22}
23
24QScriptValue WebSocketServerClass::constructor(QScriptContext* context, QScriptEngine* engine) {
25 // the serverName is used in handshakes
26 QString serverName = QStringLiteral("HighFidelity - Scripted WebSocket Listener");
27 // port 0 will auto-assign a free port
28 quint16 port = 0;
29 QScriptValue callee = context->callee();
30 if (context->argumentCount() > 0) {
31 QScriptValue options = context->argument(0);
32 QScriptValue portOption = options.property(QStringLiteral("port"));
33 if (portOption.isValid() && portOption.isNumber()) {
34 port = portOption.toNumber();
35 }
36 }
37 return engine->newQObject(new WebSocketServerClass(engine, serverName, port));
38}
39
40WebSocketServerClass::~WebSocketServerClass() {
41 if (_webSocketServer.isListening()) {
42 _webSocketServer.close();
43 }
44}