Kaydet (Commit) 65f45a27 authored tarafından Abdülkerim AKSAK's avatar Abdülkerim AKSAK

odadan ayrilma eklendi

odaya katılma ve oda olusturma fonksiyon hataları giderildi
üst d393a461
......@@ -39,3 +39,33 @@ odaOlusturBtn.onclick = () => {
});
odaOlusturContainer.appendChild(elemt3);
};
// ODAYA KATIL
const odayaKatilContainer = document.getElementById("odayaKatil");
const odayaKatilBtn = document.getElementById("odayaKatilBtn");
const odayaKatilInput = document.getElementById("odayaKatilInput");
odayaKatilBtn.onclick = () => {
let roomKey = odayaKatilInput.value;
game.emit("joinRoom", roomKey, "Muaz"); //username dinamik olmalı!
let elemt = document.createElement("p");
game.on("success", (roomKey) => {
elemt.textContent = roomKey + " odasına giriş yapıldı.";
});
game.on("err", (roomKey) => {
elemt.textContent = roomKey + " odasına giriş yapılamadı.";
});
odayaKatilContainer.appendChild(elemt);
};
// ODADAN AYRIL
const odadanAyrilBtn = document.getElementById("odadanAyrilBtn");
odadanAyrilBtn.onclick = () => {
let roomKey = odayaKatilInput.value;
game.emit("disconnection", roomKey);
let elemt = document.createElement("p");
game.on("disconnectionRoom", (key) => {
elemt.textContent = key + " odasından ayrıldın.";
});
odayaKatilContainer.appendChild(elemt);
};
......@@ -14,7 +14,7 @@ io.of("/game").on("connection", (socket) => {
// Oda Oluşturma
socket.on("odaOlustur", function (roomKey) {
gameRooms.push(roomKey); //oda için oluşturulan key
console.log(`"${roomKey}" odasi olusturuldu.`);
console.log(`odaOlustur..: "${roomKey}" odasi olusturuldu.`);
return socket.emit("odaBasariliSekildeOlusturuldu", roomKey);
});
......@@ -22,14 +22,27 @@ io.of("/game").on("connection", (socket) => {
socket.on("joinRoom", (roomKey, username) => {
if (gameRooms.includes(roomKey)) {
socket.join(roomKey);
io.of("/chat").in(roomKey).emit("newUser", username); //odaya biri girdiğinde tetiklenir
console.log(`"${roomKey}" odasina "${username} giris yapti.`);
return socket.emit("success", roomKey); //eğer gameRooms içinde oda varsa (1)
io.of("/game").in(roomKey).emit("newUser", username); //odaya biri girdiğinde tetiklenir
console.log(`joinRoom..: "${roomKey}" odasina "${username} giris yapti.`);
return socket.emit("success", roomKey);
} else {
console.log(`"${roomKey}" odasina "${username}" giris yapilamadi.`);
console.log(
`joinRoom..: "${roomKey}" odasina "${username}" giris yaptilamadi.`
);
return socket.emit("err", roomKey);
}
});
// Odadan ayrılma
//? username
socket.on("disconnection", (roomKey) => {
console.log(`disconnection...: "${roomKey}" odasindan ayrildi.`);
let index = gameRooms.indexOf(roomKey); //roomKey'e ait index değeri
if (index > -1) {
gameRooms.splice(index, 1); // odadan ayrilma gerceklestiginde oda silinir
}
return socket.emit("disconnectionRoom", roomKey);
});
});
http.listen(port, () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment