0
I was making a matchmaking system for my online game, and I needed to warn these two users that they connected via this system, and the most effective way I thought, was to filter these users via their ID, but with my code it always ends up catching the last user who connected, so I wanted to know if there are any native commands for this.
Javascript
<script>
var nome = location.href.replace("http://henriqueneimog.ddns.net/",'')
io = io('http://henriqueneimog.ddns.net');
io.on('Wait', () => {
document.getElementById('Esperando').innerHTML = '<h1>Aguardando Encontrar Jogador</h1>'
})
io.on('user', () => {
console.log('Conectado Com Sucesso')
io.emit('nome', nome)
})
function FazerConexão(nome){
io.emit('StartConnection', nome)
}
io.on('Avisar', texto => {
document.getElementById('Esperando').innerText = texto
})
</script>
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sistema Matchmacking</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.1.0/socket.io.js"></script>
</head>
<body>
<button onclick="FazerConexão(nome)">Entrar em uma Partida</button><br>
<div id='Esperando'></div>
</body>
</html>
Node.js
const express = require('express');
const path = require('path');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'public'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use('/', (req, res) =>{
res.render('index.html');
});
server.listen(4000);
matchmakingSystem = []
setInterval( () => {
if(matchmakingSystem.length == 2){
avisarSocket(matchmakingSystem[0], matchmakingSystem[1])
}
}, 100)
io.on('connection', socket => {
avisarSocket = function(id1, id2){
if(socket.id == id1.Código || socket.id == id2.Código){//Filtro que eu tentei fazer
socket.emit('Avisar', 'Um Player foi achado para jogar')
}
matchmakingSystem = []
}
socket.emit('user', '')
socket.on('StartConnection', user => {
matchmakingSystem.push({Código: socket.id, Usuario:user})
console.log(matchmakingSystem)
socket.emit('Wait', '')
})
socket.on('nome', user => {
console.log('O ID: '+socket.id+' Foi conectado como '+user)
console.log(matchmakingSystem)
})
socket.on('disconnect', () => {
console.log(socket.id+' desconectado')
var NumberWhile = 0
while(NumberWhile < matchmakingSystem.length){
if(matchmakingSystem[NumberWhile].Código == socket.id){
matchmakingSystem.splice(NumberWhile, NumberWhile+1)
console.log(matchmakingSystem)
break
}
NumberWhile++
}
})
})