wip chat adding room
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
const add_user = (socket, usernames, username) => {
|
||||
const add_user = (socket, username, usernames) => {
|
||||
|
||||
//console.log(`adduser: ${username}, ${usernames[username]}`);
|
||||
|
||||
// store the username in the socket session for this client
|
||||
socket.username = username;
|
||||
// store the room name in the socket session for this client
|
||||
@@ -11,7 +14,7 @@ const add_user = (socket, usernames, username) => {
|
||||
socket.emit('updatemsg', 'SERVER', 'you have connected to room1');
|
||||
// echo to room 1 that a person has connected to their room
|
||||
socket.broadcast.to('room1').emit('updatemsg', 'SERVER', username + ' has connected to this room');
|
||||
//socket.emit('updaterooms', rooms, 'room1');
|
||||
socket.emit('updaterooms', rooms, 'room1');
|
||||
};
|
||||
|
||||
module.exports = add_user;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const send_msg = (socket, msg) => {
|
||||
// console.log('message received: ' + msg);
|
||||
//console.log('message from: ' + socket.username);
|
||||
//console.log('message received: ' + msg);
|
||||
socket.to(socket.room).emit('updatemsg', socket.username, msg);
|
||||
};
|
||||
|
||||
|
||||
@@ -19,20 +19,18 @@ let rooms = ['room1', 'room2', 'room3'];
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
|
||||
console.log('a user is connected');
|
||||
|
||||
socket.on('adduser', (username) => {
|
||||
add_user(socket, username);
|
||||
add_user(socket, username, usernames);
|
||||
});
|
||||
|
||||
socket.on('sendmsg', (msg) => {
|
||||
send_msg(socket, msg);
|
||||
});
|
||||
|
||||
// socket.on('sendmsg', function (data) {
|
||||
// // we tell the client to execute 'updatechat' with 2 parameters
|
||||
// io.sockets.in(socket.room).emit('updatemsg', socket.username, data);
|
||||
// });
|
||||
socket.on('joinlastroom', () => {
|
||||
if (socket.room !== null)
|
||||
socket.emit('updateroom', socket.room);
|
||||
});
|
||||
|
||||
socket.on('switchroom', function(newroom){
|
||||
// leave the current room (stored in session)
|
||||
|
||||
Reference in New Issue
Block a user