NodeChat

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 0d7f179b25103c403e32173247c3c1c9df686052
parent e2530b23ffc9c17c3a1a3767277b6410801e7d94
Author: underd0g1 <hide4@comcast.net>
Date:   Wed, 23 Sep 2020 21:05:21 -0400

added base

Diffstat:
Mindex.html | 9++++++++-
Mindex.js | 17+++++++++++++++--
2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/index.html b/index.html @@ -1,7 +1,7 @@ <!doctype html> <html> <head> - <title>Socket.IO chat</title> + <title>NodeChat</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { font: 13px Helvetica, Arial; } @@ -33,7 +33,14 @@ $('#messages').append($('<li>').text(msg)); window.scrollTo(0, document.body.scrollHeight); }); + // append text if someone is online + socket.on('is_online', function(username) { + $('#messages').append($('<li>').html(username)); + }); + var username = prompt('Please tell me your name'); + socket.emit('username', username); }); + </script> </body> </html> diff --git a/index.js b/index.js @@ -8,11 +8,24 @@ app.get('/', function(req, res){ }); io.on('connection', function(socket){ + socket.on('username', function(username){ + socket.username = username; + var address = socket.request.connection.remoteAddress; + io.emit('is_online', '🔵 <i>' + socket.username + ' (' + address + ') joined the chat..</i>'); + }); + + + socket.on('disconnect', function(username) { + io.emit('is_online', '🔴 <i>' + socket.username + ' left the chat..</i>'); + }); + socket.on('chat message', function(msg){ - io.emit('chat message', msg); + + io.emit('chat message','▒ '+ socket.username + ': ' + msg); }); + }); http.listen(port, function(){ - console.log('listening on *:' + port); + console.log('you are now chatting on port ' + port); });