Move WebSocketServer to be accessable from Assignment-Client:Agent only.

This commit is contained in:
Thijs Wenker 2015-08-21 22:43:49 +02:00
parent d3a6833133
commit d89ecc1e42
3 changed files with 42 additions and 39 deletions

View file

@ -24,6 +24,7 @@
#include <SoundCache.h> #include <SoundCache.h>
#include <UUID.h> #include <UUID.h>
#include <WebSocketServerClass.h>
#include <EntityScriptingInterface.h> // TODO: consider moving to scriptengine.h #include <EntityScriptingInterface.h> // TODO: consider moving to scriptengine.h
#include "avatars/ScriptableAvatar.h" #include "avatars/ScriptableAvatar.h"
@ -184,6 +185,9 @@ void Agent::run() {
_scriptEngine.registerGlobalObject("SoundCache", DependencyManager::get<SoundCache>().data()); _scriptEngine.registerGlobalObject("SoundCache", DependencyManager::get<SoundCache>().data());
QScriptValue webSocketServerConstructorValue = _scriptEngine.newFunction(WebSocketServerClass::constructor);
_scriptEngine.globalObject().setProperty("WebSocketServer", webSocketServerConstructorValue);
auto entityScriptingInterface = DependencyManager::get<EntityScriptingInterface>(); auto entityScriptingInterface = DependencyManager::get<EntityScriptingInterface>();
_scriptEngine.registerGlobalObject("EntityViewer", &_entityViewer); _scriptEngine.registerGlobalObject("EntityViewer", &_entityViewer);

View file

@ -61,40 +61,43 @@ unitTests.addTest("Test WebSocket invalid URL", function(finished) {
this.assertEquals(WEBSOCKET_INVALID_URL, webSocket.url, "url should be '" + WEBSOCKET_INVALID_URL + "'"); this.assertEquals(WEBSOCKET_INVALID_URL, webSocket.url, "url should be '" + WEBSOCKET_INVALID_URL + "'");
}, UNITTEST_TIMEOUT); }, UNITTEST_TIMEOUT);
if (this.WebSocketServer === undefined) {
unitTests.addTest("Test WebSocketServer with three clients", function(finished) { print("Skipping WebSocketServer tests.");
var _this = this; } else {
const NUMBER_OF_CLIENTS = 3; unitTests.addTest("Test WebSocketServer with three clients", function(finished) {
var connectedClients = 0; var _this = this;
var respondedClients = 0; const NUMBER_OF_CLIENTS = 3;
var webSocketServer = new WebSocketServer(); var connectedClients = 0;
_this.assertEquals(true, webSocketServer.listening, "listening should be true"); var respondedClients = 0;
webSocketServer.newConnection.connect(this.registerCallbackFunction(function(newClient) { var webSocketServer = new WebSocketServer();
connectedClients++; _this.assertEquals(true, webSocketServer.listening, "listening should be true");
newClient.onmessage = _this.registerCallbackFunction(function(event) { webSocketServer.newConnection.connect(this.registerCallbackFunction(function(newClient) {
var data = JSON.parse(event.data); connectedClients++;
_this.assertEquals(TEST_MESSAGE, data.message, "data.message should be '" + TEST_MESSAGE + "'"); newClient.onmessage = _this.registerCallbackFunction(function(event) {
respondedClients++; var data = JSON.parse(event.data);
if (respondedClients === NUMBER_OF_CLIENTS) { _this.assertEquals(TEST_MESSAGE, data.message, "data.message should be '" + TEST_MESSAGE + "'");
webSocketServer.close(); respondedClients++;
_this.assertEquals(false, webSocketServer.listening, "listening should be false"); if (respondedClients === NUMBER_OF_CLIENTS) {
_this.done(); webSocketServer.close();
} _this.assertEquals(false, webSocketServer.listening, "listening should be false");
}); _this.done();
newClient.send(JSON.stringify({message: TEST_MESSAGE, client: connectedClients})); }
})); });
var newSocket1 = new WebSocket(webSocketServer.url); newClient.send(JSON.stringify({message: TEST_MESSAGE, client: connectedClients}));
newSocket1.onmessage = this.registerCallbackFunction(function(event) { }));
newSocket1.send(event.data); var newSocket1 = new WebSocket(webSocketServer.url);
}); newSocket1.onmessage = this.registerCallbackFunction(function(event) {
var newSocket2 = new WebSocket(webSocketServer.url); newSocket1.send(event.data);
newSocket2.onmessage = this.registerCallbackFunction(function(event) { });
newSocket2.send(event.data); var newSocket2 = new WebSocket(webSocketServer.url);
}); newSocket2.onmessage = this.registerCallbackFunction(function(event) {
var newSocket3 = new WebSocket(webSocketServer.url); newSocket2.send(event.data);
newSocket3.onmessage = this.registerCallbackFunction(function(event) { });
newSocket3.send(event.data); var newSocket3 = new WebSocket(webSocketServer.url);
}); newSocket3.onmessage = this.registerCallbackFunction(function(event) {
}, UNITTEST_TIMEOUT); newSocket3.send(event.data);
});
}, UNITTEST_TIMEOUT);
}
unitTests.run(); unitTests.run();

View file

@ -39,7 +39,6 @@
#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"
@ -348,9 +347,6 @@ 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);