]> git.cworth.org Git - zombocom-ai/commitdiff
Add a (half)-functional comment field
authorCarl Worth <cworth@cworth.org>
Wed, 7 Dec 2022 04:18:59 +0000 (20:18 -0800)
committerCarl Worth <cworth@cworth.org>
Wed, 7 Dec 2022 04:18:59 +0000 (20:18 -0800)
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.

index.html
index.js

index 5a74b471a44353b74de7cb0049532527be4453d1..b7b82de7068bedb0c4b3f1910ee11c2e78c2867d 100644 (file)
       </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>
@@ -100,6 +106,17 @@ mute.addEventListener("click", () => {
   <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>
index 336f36ffd8fe0af1a610bfb406f92d498a505f46..3ffea3d9feef95ac81ac8a66c2b5c68badc018e9 100644 (file)
--- a/index.js
+++ b/index.js
@@ -12,9 +12,11 @@ app.get('/index.html', (req, res) => {
 
 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}`);
 });
-