Created
February 9, 2024 19:54
-
-
Save benixal/0c811339111dbbabdb92c1e6b9354de6 to your computer and use it in GitHub Desktop.
WebSocket HTML Chat Client and Server
This file contains hidden or 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 lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>WebSocket Simple Chat</title> | |
<style> | |
#chatbox { | |
width: 200px; | |
height: 100px; | |
border: 2px solid gray; | |
padding: 1rem; | |
display: flex; | |
flex-direction: column; | |
overflow: scroll; | |
} | |
.admin { | |
align-self: flex-start; | |
background-color: blue; | |
color: white; | |
padding: 0.5rem; | |
margin: 0.5rem; | |
} | |
.client { | |
align-self: flex-end; | |
background-color: brown; | |
color: white; | |
padding: 0.5rem; | |
margin: 0.5rem; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="chatbox"></div> | |
<input type="text" id="msg" placeholder="Message..."> | |
<button id="send">Send</button> | |
<script> | |
const messageInput = document.getElementById("msg"); | |
const btnSend = document.getElementById("send"); | |
const chatBox = document.getElementById("chatbox"); | |
const myWebSocket = new WebSocket("ws://localhost:4600"); | |
myWebSocket.addEventListener("message", (serverMsg) => { | |
chatBox.innerHTML += '<div class="admin">' + serverMsg.data + '</div>'; | |
chatBox.scrollTop = chatBox.scrollHeight; | |
}) | |
const sendMessage = () => { | |
myWebSocket.send(messageInput.value); | |
chatBox.innerHTML += '<div class="client">' + messageInput.value + '</div>'; | |
chatBox.scrollTop = chatBox.scrollHeight; | |
messageInput.value = ""; | |
}; | |
btnSend.addEventListener("click",sendMessage); | |
messageInput.addEventListener("keyup", (event) => { | |
if (event.key == "Enter") { | |
sendMessage() | |
} | |
}) | |
</script> | |
</body> | |
</html> |
This file contains hidden or 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
// Simple WebSocket | |
/* | |
1.install ws: | |
npm install ws | |
2. run: | |
node server.js | |
*/ | |
const WebSocket = require("ws"); | |
const server = new WebSocket.Server({ | |
host: "localhost", | |
port: 4600 | |
}); | |
console.log("Server is Ready!"); | |
server.on("connection" , (socket) => { | |
client = socket; | |
console.log("Someone is connected!"); | |
socket.on("message" , (message) => { | |
console.log("Client:" , message.toString()); | |
}); | |
}); | |
process.stdin.on("data" , (data)=> { | |
client.send(data.toString().trim()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment