This sends the comment from the client to the server (but not back again).
The server process simply prints the comment out on its console.
</form>
</p>
+ <p>
+ <form action="" id="comment-form">
+ <input id="comment" type="text" style="width:100%" autocomplete="off" placeholder="Add a comment" />
+ </form>
+ </p>
+
<div align="center">
<div class="animate-flicker">
<p>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
+
+ var form = document.querySelector("#comment-form");
+ var comment = document.querySelector("#comment");
+
+ form.addEventListener('submit', function(e) {
+ e.preventDefault();
+ if (comment.value) {
+ socket.emit('comment', comment.value);
+ comment.value = '';
+ }
+ });
</script>
</body>
</html>
io.on('connection', (socket) => {
console.log('a user connected');
+ socket.on('comment', (comment) => {
+ console.log('comment: ' + comment);
+ });
});
server.listen(port, () => {
console.log(`listening on *:${port}`);
});
-