Kaydet (Commit) 3f9b1d4e authored tarafından Abdülkerim AKSAK's avatar Abdülkerim AKSAK

mesaj yollama fonksiyonu olusturuldu

Kullanicilarin bulundukları odada iletişim kurmaları saglanmıstır
üst 65f45a27
......@@ -69,3 +69,31 @@ odadanAyrilBtn.onclick = () => {
});
odayaKatilContainer.appendChild(elemt);
};
// MESAJ YOLLAMA <OdayaKatıl>
function sendMessage() {
let odayaKatilInput = document.getElementById("odayaKatilInput");
let roomKey = odayaKatilInput.value; // Odaya katıl alanı için etkin
let input = document.getElementById("mesajGonderOdayaKatilInput");
let msg = input.value;
input.value = "";
game.emit("newMsg", "Muaz", msg, roomKey);
}
const mesajGonderBtnOdayaKatil = document.getElementById(
"mesajGonderBtnOdayaKatil"
);
mesajGonderBtnOdayaKatil.onclick = () => {
sendMessage();
};
// MESAJI ALMA <OdaOlustur>
function addMessage(container, username, msg) {
let elemt = document.createElement("p");
elemt.textContent = `${username}:${msg}`;
container.appendChild(elemt);
}
game.on("newMessage", (username, msg) => {
console.log(`newMessage...: "${username}" kisisinden "${msg}" mesajı geldi.`);
addMessage(odaOlusturContainer, username, msg);
});
......@@ -23,7 +23,9 @@ io.of("/game").on("connection", (socket) => {
if (gameRooms.includes(roomKey)) {
socket.join(roomKey);
io.of("/game").in(roomKey).emit("newUser", username); //odaya biri girdiğinde tetiklenir
console.log(`joinRoom..: "${roomKey}" odasina "${username} giris yapti.`);
console.log(
`joinRoom..: "${roomKey}" odasina "${username}" giris yapti.`
);
return socket.emit("success", roomKey);
} else {
console.log(
......@@ -43,6 +45,14 @@ io.of("/game").on("connection", (socket) => {
}
return socket.emit("disconnectionRoom", roomKey);
});
//Mesaj gönderme
socket.on("newMsg", (username, msg, roomKey) => {
console.log(
`newMsg...: "${username}" kisisinden "${roomKey}" odasindan "${msg}" mesaji yollandi.`
);
socket.broadcast.to(roomKey).emit("newMessage", username, msg); //oda içindeki her user için gönderilir.
});
});
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