Question
Please help! This project is based on an open source Node.js application posted at: http://socket.io/get-started/chat/. Your job is to modify the index.js page so that
Please help!
This project is based on an open source Node.js application posted at: http://socket.io/get-started/chat/. Your job is to modify the index.js page so that the app will have two new features:
- Whenever someone joins or leaves the room, a message will be displayed to all users.
- Instead of having a username, each user is assigned a socket generated session id when he/she first joins
Hint:
- You can download this project source code from https://github.com/rauchg/chat-example or use the file provided by your instructor in BlackBoard: unit09_lab.zip.
- When a user joins the chat room, you need to register the connect event. For example:
io.on(connect, function(socket)) {
io.emit(chat message, --- someone just joined the room!!!);
});
- When a user leaves the chat room, you need to register the disconnect event on that particular socket. For example:
io.on(connection, function(socket) {
socket.on(disconnect, function() {
io.emit(chat message, --- someone just left the room!!!);
});
});
To get the session Id from each user, you can use socket.client.id property. For example: io.emit(chat message, --- + socket.client.id + just joined the room!!!);
index.js
var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); app.get('/', function(req, res){ res.sendFile(__dirname + '/index.html'); }); io.on('connection', function(socket){ socket.on('chat message', function(msg){ io.emit('chat message', msg); }); }); http.listen(3000, function(){ console.log('listening on *:3000'); });
index.html
Socket.IO chat
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started