Created
February 18, 2019 10:01
-
-
Save dkhd/d8db5c13583ee9c2b862ec487380387d to your computer and use it in GitHub Desktop.
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>Simple Group Chat on Node.js</title> | |
<style> | |
* { margin: 0; padding: 0; box-sizing: border-box; } | |
body { font: 13px Helvetica, Arial; } | |
form { background: #fff; padding: 3px; position: fixed; bottom: 0; width: 100%; border-color: #000; border-top-style: solid; border-top-width: 1px;} | |
form input { border-style: solid; border-width: 1px; padding: 10px; width: 85%; margin-right: .5%; } | |
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; margin-left: 2%; } | |
#messages { list-style-type: none; margin: 0; padding: 0; } | |
#messages li { padding: 5px 10px; } | |
#messages li:nth-child(odd) { background: #eee; } | |
</style> | |
<script src="../../socket.io/socket.io.js"></script> | |
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> | |
</head> | |
<body> | |
<ul id="messages"></ul> | |
<form action="/" method="POST" id="chatForm"> | |
<input id="txt" autocomplete="off" autofocus="on" oninput="isTyping()" placeholder="type your message here..." /><button>Send</button> | |
</form> | |
<script> | |
var socket = io.connect('http://localhost:8080'); | |
// submit text message without reload/refresh the page | |
$('form').submit(function(e){ | |
e.preventDefault(); // prevents page reloading | |
socket.emit('chat_message', $('#txt').val()); | |
$('#txt').val(''); | |
return false; | |
}); | |
// append the chat text message | |
socket.on('chat_message', function(msg){ | |
$('#messages').append($('<li>').html(msg)); | |
}); | |
// append text if someone is online | |
socket.on('is_online', function(username) { | |
$('#messages').append($('<li>').html(username)); | |
}); | |
// ask username | |
var username = prompt('Please tell me your name'); | |
socket.emit('username', username); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment