Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

409
Vistas
How can I disconnect clients with socket io?

I am trying to write an application in which clients share real-time location with each other with javascript. It closes on the server.js side, but I can't see it on the client side of the client.

Server.js

const server = require('http').Server(app);
const io = require('socket.io')(server);
server.listen(8080)
const location =[];
const players ={}

io.on('connection',(socket)=>{
    socket.on('Client_send_pos',(data)=>{ /* position verisi alınır. */
    console.log("new client connected, with id " + socket.id);
    location.push(data); /* location dizine aktarılır.*/
    io.emit('send_mang_pos',location); /* emit edilerek yazdırılması sağlanır. */             
});
        
socket.on("disconnect",() => {
    console.log(`${data} User disconnected.`)
    const index = location.indexOf(data); /* disconnect olan browser'ın dizide kaçıncı eleman olduğu bulu. */
    if (index != -1) {
        delete location[index];
        location.splice(index, 1);
        io.emit('delete',index);
        console.log(index);
    }
    io.emit('send_mang_pos',location);           
});

Client

let socket = io.connect('http://localhost:8080');
const position = [this.state.location.x, this.state.location.y]
socket.emit('Client_send_pos',position)
const map = L.map('map').setView(position, 10);
        
socket.on('send_mang_pos',(data)=>{
    data.forEach((i,y,z)=>{
                
    });
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I've written something similar: https://github.com/armonkahil/Dismissed/blob/master/client/src/components/Buttons/index.js It was my understanding that socket.io handled that itself. When the client leaves, it will emit a "disconnect" event. I never wrote a disconnect emitter. I just made sure to listen for it on the server-side like you already have set up.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda