Client auth intent

This commit is contained in:
Ben Kyd
2021-02-20 23:44:15 +00:00
parent b6867e412d
commit 095a0efbc1
3 changed files with 54 additions and 15 deletions

View File

@@ -49,18 +49,18 @@ socket.on('identify', (...args) => {
return;
}
socket.emit('identify', { userid: user.uid });
socket.emit('identify', { userid: user.uid, intent: 'LOBYING' });
ConnectionState.innerHTML = 'Identify response';
});
socket.on('identify-success', (...args) => {
console.log(args);
ConnectionState.innerHTML = JSON.stringify(args[0]);
ConnectionState.innerHTML = JSON.stringify(args[0], undefined, 4);
});
socket.on('identify-error', (...args) => {
console.log(args);
ConnectionState.innerHTML = JSON.stringify(args[0]);
ConnectionState.innerHTML = JSON.stringify(args[0], undefined, 4);
});

View File

@@ -8,7 +8,11 @@ USER OBJECT
{
username: username,
uid: id,
ip: ip
ip: ip,
// REGISTERED, CONNECTED, DISCONNECTED, INGAME
state: 'REGISTERED',
// Doesn't update if state changes
connectionid: 'none',
}
NOTES
- Socket relations are handled by the socket server in order
@@ -62,6 +66,7 @@ function GetUserByUsername(username)
for (const user in OnlineUsers)
if (OnlineUsers[user].username === username)
return OnlineUsers[user];
return false;
}
@@ -74,7 +79,7 @@ function RegisterUser(username, ip)
username: username,
uid: uid,
ip: ip,
// REGISTERED, CONNECTED, DISCONNECTED
// REGISTERED, CONNECTED, DISCONNECTED, INGAME
state: 'REGISTERED',
// Doesn't update if state changes
connectionid: 'none',
@@ -86,8 +91,21 @@ function RegisterUser(username, ip)
}
// Can return string errors, or true if success
// yes multiple return types i know its bad
function UserConnectionExists(userid)
{
if (OnlineUsers[userid].state === 'CONNECTED') return true;
}
function GetUserbyConnection(connectionid)
{
for (const user in OnlineUsers)
if (OnlineUsers[user].connectionid === connectionid)
return OnlineUsers[user];
return false;
}
function UserConnect(userid, connectionid)
{
if (OnlineUsers[userid].state === 'CONNECTED') return 'User Connected';
@@ -115,6 +133,11 @@ module.exports = {
RegisterUser: RegisterUser,
UserConnectionExists: UserConnectionExists,
GetUserbyConnection: GetUserbyConnection,
UserConnect: UserConnect,
UserDisconnect: UserDisconnect
}

View File

@@ -16,6 +16,10 @@ const Error = require('./error.js');
* Sockets will error with HTTP error codes because it's a pretty decent
* standard for standard errors that may occur. Then general errors will
* be 500 errors with a description
*
* Clients connect to identify with an 'intent' to be placed in a game or to
* be part of the lobbying system, the intent of the client defines what
* sort of communication they will be recieving
*/
function init()
@@ -41,7 +45,7 @@ async function Router(socket)
socket.emit('identify');
socket.on('identify', user => ClientIdentify(socket, user.userid));
socket.on('identify', args => ClientIdentify(socket, args));
socket.on('disconnect', args => HandleDisconnect(socket, ...args));
@@ -49,21 +53,29 @@ async function Router(socket)
}
function ClientIdentify(socket, userid)
function ClientIdentify(socket, args)
{
const err = new Error;
const user = Game.Registrar.GetUserByUID(userid);
const user = Game.Registrar.GetUserByUID(args.userid);
const intent = args.intent;
if (!intent)
{
err.addError(403, 'Forbidden', 'Client has no intent');
socket.emit('identify-error', err.toError);
return;
}
if (!user)
{
err.addError(403, 'Forbidden', 'Unknown uid');
socket.emit('identify-error', err.toError);
return;
}
console.log(user);
// TODO: Sort out client intent
const status = Game.Registrar.UserConnect(userid, socket.id);
if (status === true)
@@ -72,6 +84,7 @@ function ClientIdentify(socket, userid)
return;
} else
{
// TODO: wrong error
err.addError(500, 'Internal Server Error', 'Socket busy');
socket.emit('identify-error', err.toError);
return;
@@ -80,7 +93,10 @@ function ClientIdentify(socket, userid)
function HandleDisconnect(socket, args)
{
Logger.debug(`${socket.id} DISCONNECTED`)
Logger.debug(`${socket.id} DISCONNECTED`);
const user = Game.Registrar.GetUserbyConnection(socket.id);
if (!user) return;
Game.Registrar.UserDisconnect(user.id, socket.id);
}