]> git.cworth.org Git - zombocom-ai/commitdiff
Add the second half necessary for functional comments
authorCarl Worth <cworth@cworth.org>
Wed, 7 Dec 2022 04:24:27 +0000 (20:24 -0800)
committerCarl Worth <cworth@cworth.org>
Wed, 7 Dec 2022 04:29:29 +0000 (20:29 -0800)
The server emits the comment back to all clients and the client appends
to a <ul> item (through simple DOM manipulations).

index.html
index.js

index b7b82de7068bedb0c4b3f1910ee11c2e78c2867d..167c97ef840e51dc69d19157a06cc156acda7875 100644 (file)
@@ -64,6 +64,9 @@
       </form>
     </p>
 
+    <ul id="comments">
+    </ul>
+
     <p>
       <form action="" id="comment-form">
         <input id="comment" type="text" style="width:100%" autocomplete="off" placeholder="Add a comment" />
@@ -107,6 +110,7 @@ mute.addEventListener("click", () => {
   <script>
     var socket = io();
 
+    var comments = document.querySelector("#comments");
     var form = document.querySelector("#comment-form");
     var comment = document.querySelector("#comment");
 
@@ -117,6 +121,12 @@ mute.addEventListener("click", () => {
             comment.value = '';
         }
     });
+
+    socket.on('comment', function(msg) {
+        var item = document.createElement('li');
+        item.textContent = msg;
+        comments.appendChild(item);
+    });
   </script>
 </body>
 </html>
index 3ffea3d9feef95ac81ac8a66c2b5c68badc018e9..27b37147c1796569df0e625d8b88db2998e7175a 100644 (file)
--- a/index.js
+++ b/index.js
@@ -11,9 +11,8 @@ app.get('/index.html', (req, res) => {
 });
 
 io.on('connection', (socket) => {
-    console.log('a user connected');
-    socket.on('comment', (comment) => {
-        console.log('comment: ' + comment);
+    socket.on('comment', (msg) => {
+        io.emit('comment', msg);
     });
 });