Commit a5ce9064 authored by Hubert Denkmair's avatar Hubert Denkmair
Browse files

check and set viewer keys

parent 318a2880
Loading
Loading
Loading
Loading
+22 −4
Original line number Diff line number Diff line
#include "RelayServer.h"
#include <iostream>
#include <string>
#include <TcpServer/EPoll.h>
#include <nlohmann/json.hpp>
using nlohmann::json;

RelayServer::RelayServer()
{
@@ -35,11 +38,11 @@ int RelayServer::Run()
					con->FrameComplete(frame_id, _tcpProtocol);

					auto key = con->getViewerKey();
					//if (key!=0) TODO implement
					if (key!=0)
					{
						for (auto& item: logMessages)
						{
							//if (item->viewer_key == key) // TODO implement
							if (item.viewer_key == key)
							{
								con->LogMessage(frame_id, item.message);
							}
@@ -69,7 +72,22 @@ int RelayServer::Run()

	h.onMessage([](uWS::WebSocket<uWS::SERVER> *ws, char *message, size_t length, uWS::OpCode opCode)
	{	
		//ws->send(message, length, opCode);
		auto *con = static_cast<WebsocketConnection*>(ws->getUserData());

		if (length>MAX_CLIENT_MESSAGE_SIZE)
		{
			return;
		}

		std::string s(message, length);
		json data = json::parse(s, nullptr, false);
		if (!data.is_object()) { return; }

		if (data["viewer_key"].is_string())
		{
			std::string key = data["viewer_key"];
			con->setViewerKey(static_cast<uint64_t>(std::stol(key)));
		}
	});

	std::string response = "Hello!";
+1 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ class RelayServer
		static constexpr const char* ENV_GAMESERVER_PORT_DEFAULT = "9010";
		static constexpr const char* ENV_WEBSOCKET_PORT = "WEBSOCKET_PORT";
		static constexpr const char* ENV_WEBSOCKET_PORT_DEFAULT = "9009";
		static constexpr const size_t MAX_CLIENT_MESSAGE_SIZE = 10*1024;
		static int connectTcpSocket(const char* hostname, const char* port);
		static const char* getEnvOrDefault(const char* envVar, const char* defaultValue);
};
+1 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ class WebsocketConnection
		void LogMessage(uint64_t frame_id, const std::string& message);
		void sendString(std::string data);
		uint64_t getViewerKey() { return _viewerKey; }
		void setViewerKey(uint64_t key) { _viewerKey = key; }

	private:
		uWS::WebSocket<uWS::SERVER> *_websocket;