Created
December 23, 2018 11:55
-
-
Save talentlessguy/3ffda42b5c3a1a67fbbbd9044e0aa0f4 to your computer and use it in GitHub Desktop.
Socket.io Chat
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 app = require('express')() | |
const http = require('http').Server(app) | |
const io = require('socket.io')(http) | |
app.get('/', | |
(req, res) => res.sendFile( | |
__dirname + '/index.html' | |
) | |
) | |
io.on('connection', socket => { | |
socket.on('message', msg => { | |
console.log('message: ' + msg) | |
}) | |
}) | |
http.listen(3000) |
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> | |
<meta charset="utf-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<title>Page Title</title> | |
<style> | |
* { margin: 0; padding: 0; box-sizing: border-box; } | |
body { font: 13px Helvetica, Arial; } | |
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; } | |
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; } | |
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; } | |
#messages { list-style-type: none; margin: 0; padding: 0; } | |
#messages li { padding: 5px 10px; } | |
#messages li:nth-child(odd) { background: #eee; } | |
</style> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<script src="https://code.jquery.com/jquery-1.11.1.js"></script> | |
<script src="https://cdn.socket.io/socket.io-1.0.0.js"></script> | |
<script> | |
$(() => { | |
const socket = io() | |
$('form').submit(() => { | |
socket.emit('message', $('#m').val()) | |
$('#m').val('') | |
return false | |
}) | |
}) | |
</script> | |
</head> | |
<body> | |
<ul id="messages"></ul> | |
<form action=""> | |
<input id="m" autocomplete="off" /><button>Send</button> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment