I am trying to create a waiting lobby in which when i fire a post request on the api it stores the data of the player and then wait for another player to join. when new player joins then it pairs the players and then returns them in the the response of both players.
But problem i am facing is that when i am firing the request from the first player it is not waiting and instantly returning 404 error.
This is the code:-
this is the function that runs when the api is called:-
startGame(ctx) {
const player = ctx.request.body;
addToPlayers(player);
messageBus.on('match', (data) => {
return data;
})}
this is the helper functions and emitters:-
var players = [];
var EventEmitter = require('events').EventEmitter;
var messageBus = new EventEmitter();
messageBus.setMaxListeners(100);
// function to manage players array
function addToPlayers(player) {
if (!player) {
return;
}
players.push(player)
if (players.length >= 2) {
const player1 = players.shift();
const player2 = players.shift();
messageBus.emit('match', {player1, player2});
}}
the post request consists of:-
{
“id”: “1”,
“amount”: “1000”
}