Created
July 18, 2021 13:18
-
-
Save prensmiskin/2fc6b6235d59fa5d656233140c65a936 to your computer and use it in GitHub Desktop.
socket.io Kadir Kasım Hoca
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const socket = io.connect(''ip adresi girin veya localhost yazın'') | |
const sender = document.getElementById('sender') | |
const message = document.getElementById('message') | |
const submitBtn = document.getElementById('submitBtn') | |
const output = document.getElementById('output') | |
const feedback = document.getElementById('feedback') | |
submitBtn.addEventListener("click", function() { | |
}); | |
submitBtn.addEventListener('click',() => { | |
socket.emit('chat',{ | |
message: message.value, | |
sender: sender.value | |
}) | |
}) | |
socket.on('chat',data => { | |
feedback.innerHTML = ""; | |
output.innerHTML += '<p><strong>' + data.sender + ' : </strong>' + data.message + '</p>' | |
message.value = ""; | |
}) | |
message.addEventListener('keypress',() => { | |
socket.emit('typing',sender.value) | |
}) | |
socket.on('typing',data => { | |
feedback.innerHTML = '<p>' + data + 'yaziyor...</p>' | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>KK Chat</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.1.3/socket.io.js" integrity="sha512-PU5S6BA03fRv1Q5fpwXjg5nlRrgdoguZ74urFInkbABMCENyx5oP3hrDzYMMPh3qdLdknIvrGj3yqZ4JuU7Nag==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> | |
<link rel="stylesheet" type="text/css" href="style.css"> | |
</head> | |
<body> | |
<div id="chat-wrap"> | |
<h2>KK Chat</h2> | |
<div id="chat-window"> | |
<div id="output"></div> | |
<div id="feedback"></div> | |
</div> | |
<input type="text" id="sender" placeholder="Ad"> | |
<input type="text" id="message" placeholder="Mesaj"> | |
<button id="submitBtn">Gönder</button> | |
</div> | |
</body> | |
<script src="chat.js" type="text/javascript"></script> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const socket = require('socket.io') | |
const app = express() | |
const server = app.listen(3168,'ip adresi girin veya localhost yazın') | |
app.use(express.static('public')) | |
const io = socket(server) | |
io.on('connection',(socket) =>{ | |
console.log(socket.id) | |
socket.on('chat',data => { | |
io.sockets.emit('chat',data) | |
}) | |
socket.on('typing',data => { | |
socket.broadcast.emit('typing',data) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
,*{margin:0;padding:0;box-sizing: border-box;} | |
input:focus,button:focus{outline:0} | |
body{ | |
font-family:'Poppins' | |
} | |
h2{ | |
font-size:23px; | |
padding:10px 20px; | |
color:#fff; | |
background-color:#0085ad; | |
} | |
#chat-wrap{ | |
max-widht:600px; | |
margin:30px auto; | |
border: 1px solid #ccc; | |
border-radius:5px; | |
} | |
#chat-window{ | |
height:400px; | |
overflow-y:auto; | |
} | |
#output p{ | |
font-size:15px; | |
padding:12px; | |
} | |
#output p:nth-child(even){ | |
background-color:#f2f2f2; | |
} | |
#feedback p{ | |
font-size:13px; | |
padding:12px; | |
} | |
#output strong{ | |
color:#0085ad; | |
} | |
input{ | |
font-family:'Poppins'; | |
padding:10px;20px; | |
border:0px; | |
border-top:1px solid #ccc; | |
display:block; | |
widht:100%; | |
font-size:15px; | |
} | |
button{ | |
font-size:'Poppins'; | |
background-color:#0085ad; | |
color:#fff; | |
border:0; | |
border-radius:0 0 5px 5px; | |
widht:100%; | |
padding:12px 8px; | |
font-size:17px; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment