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)=>{
});
});
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.