mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-04 00:36:14 +00:00
SocketIO authentication
This commit is contained in:
parent
266303b09d
commit
ab44ac8c15
@ -41,7 +41,7 @@ public:
|
|||||||
// Singleton pattern
|
// Singleton pattern
|
||||||
static SocketIO& Get();
|
static SocketIO& Get();
|
||||||
|
|
||||||
void Emit(SocketIORoom Room, SocketIOEvent Event, const std::string& Data);
|
void Emit(SocketIOEvent Event, const std::string& Data);
|
||||||
|
|
||||||
~SocketIO();
|
~SocketIO();
|
||||||
|
|
||||||
@ -53,7 +53,6 @@ private:
|
|||||||
void ThreadMain();
|
void ThreadMain();
|
||||||
|
|
||||||
struct Event {
|
struct Event {
|
||||||
std::string Room;
|
|
||||||
std::string Name;
|
std::string Name;
|
||||||
std::string Data;
|
std::string Data;
|
||||||
};
|
};
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
#include "Common.h"
|
#include "Common.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
//TODO Default disabled with config option
|
||||||
static std::unique_ptr<SocketIO> SocketIOInstance = std::make_unique<SocketIO>();
|
static std::unique_ptr<SocketIO> SocketIOInstance = std::make_unique<SocketIO>();
|
||||||
|
|
||||||
SocketIO& SocketIO::Get() {
|
SocketIO& SocketIO::Get() {
|
||||||
@ -11,6 +13,13 @@ SocketIO& SocketIO::Get() {
|
|||||||
SocketIO::SocketIO() noexcept
|
SocketIO::SocketIO() noexcept
|
||||||
: mThread([this] { ThreadMain(); }) {
|
: mThread([this] { ThreadMain(); }) {
|
||||||
|
|
||||||
|
mClient.socket()->on("network", [&](sio::event&e) {
|
||||||
|
if(e.get_message()->get_string() == "Welcome"){
|
||||||
|
info("SocketIO Authenticated!");
|
||||||
|
mAuthenticated = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
mClient.socket()->on("welcome", [&](sio::event&) {
|
mClient.socket()->on("welcome", [&](sio::event&) {
|
||||||
info("Got welcome from backend! Authenticating SocketIO...");
|
info("Got welcome from backend! Authenticating SocketIO...");
|
||||||
mClient.socket()->emit("onInitConnection", Application::Settings.Key);
|
mClient.socket()->emit("onInitConnection", Application::Settings.Key);
|
||||||
@ -26,23 +35,6 @@ SocketIO::~SocketIO() {
|
|||||||
mThread.join();
|
mThread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
static constexpr auto RoomNameFromEnum(SocketIORoom Room) {
|
|
||||||
switch (Room) {
|
|
||||||
case SocketIORoom::None:
|
|
||||||
return "";
|
|
||||||
case SocketIORoom::Console:
|
|
||||||
return "console";
|
|
||||||
case SocketIORoom::Info:
|
|
||||||
return "info";
|
|
||||||
case SocketIORoom::Player:
|
|
||||||
return "player";
|
|
||||||
case SocketIORoom::Stats:
|
|
||||||
return "stats";
|
|
||||||
default:
|
|
||||||
error("unreachable code reached (developer error)");
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static constexpr auto EventNameFromEnum(SocketIOEvent Event) {
|
static constexpr auto EventNameFromEnum(SocketIOEvent Event) {
|
||||||
switch (Event) {
|
switch (Event) {
|
||||||
@ -62,16 +54,15 @@ static constexpr auto EventNameFromEnum(SocketIOEvent Event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SocketIO::Emit(SocketIORoom Room, SocketIOEvent Event, const std::string& Data) {
|
void SocketIO::Emit(SocketIOEvent Event, const std::string& Data) {
|
||||||
if (!mAuthenticated) {
|
if (!mAuthenticated) {
|
||||||
debug("trying to emit a socket.io event when not yet authenticated");
|
debug("trying to emit a socket.io event when not yet authenticated");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::string RoomName = RoomNameFromEnum(Room);
|
|
||||||
std::string EventName = EventNameFromEnum(Event);
|
std::string EventName = EventNameFromEnum(Event);
|
||||||
debug("emitting event \"" + EventName + "\" with data: \"" + Data + "\" in room \"/key/" + RoomName + "\"");
|
debug("emitting event \"" + EventName + "\" with data: \"" + Data);
|
||||||
std::unique_lock Lock(mQueueMutex);
|
std::unique_lock Lock(mQueueMutex);
|
||||||
mQueue.push_back({ RoomName, EventName, Data });
|
mQueue.push_back({EventName, Data });
|
||||||
debug("queue now has " + std::to_string(mQueue.size()) + " events");
|
debug("queue now has " + std::to_string(mQueue.size()) + " events");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,7 +84,6 @@ void SocketIO::ThreadMain() {
|
|||||||
mQueue.pop_front();
|
mQueue.pop_front();
|
||||||
} // end queue lock scope
|
} // end queue lock scope
|
||||||
debug("sending \"" + TheEvent.Name + "\" event");
|
debug("sending \"" + TheEvent.Name + "\" event");
|
||||||
auto Room = "/" + TheEvent.Room;
|
|
||||||
mClient.socket()->emit(TheEvent.Name, TheEvent.Data);
|
mClient.socket()->emit(TheEvent.Name, TheEvent.Data);
|
||||||
debug("sent \"" + TheEvent.Name + "\" event");
|
debug("sent \"" + TheEvent.Name + "\" event");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user