/////SOCKET.IO///////
const io = require("socket.io")().listen(server);

const peers = {};

io.on("connection", (socket) => {
  console.log(
    "Someone joined our server using socket.io.  Their socket id is",
    socket.id
  );

  // Make sure to send the client all existing peers
  socket.emit("introduction", peers);

  // tell other clients that a new peer joined
  io.emit("newPeerConnected", socket.id);

  //
  if(!currentSong.name){
    console.log("First user");
    socket.emit("askToPlay");

  }else{
    console.log("Not first user, sending current song info");
    socket.emit("sendCurrentSong", currentSong);
    console.log(currentSong)
  }
  
  socket.on("playSong",(songName)=>{ // play 或是切歌
    // update currentSong
    currentSong = {
      name: songName,
      startTime: new Date().getTime()
    }
   
    if(songName !== " Change Song "){
    socket.broadcast.emit("sendCurrentSong",currentSong);
  }
    // socket.emit("sendCurrentSong",currentSong);
  })