wip chat adding room

This commit is contained in:
lenovo
2022-12-06 19:48:19 +01:00
parent d0b39dbb82
commit 3b60f3952c
8 changed files with 47 additions and 22 deletions

View File

@@ -75,7 +75,6 @@
<!-- https://socket.io/docs/v4/client-installation/ -->
<!-- https://socket.io/docs/v4/client-api/ -->
<script src="https://cdn.socket.io/4.5.3/socket.io.min.js" integrity="sha384-WPFUvHkB1aHA5TDSZi6xtDgkF0wXJcIIxXhC6h8OT8EH3fC5PWro5pWJ1THjcfEi" crossorigin="anonymous"></script>
<script>
const socket = io("http://localhost:3000");

View File

@@ -1,4 +1,5 @@
socket.on('connect', () => {
//socket.emit('adduser', prompt("what's your name ?"));
socket.emit('adduser', "glurk");
//socket.emit('joinlastroom');
});

View File

@@ -7,7 +7,7 @@ const send_msg = () => {
div_msg.focus();
if (msg.length > 0) {
socket.emit('sendmsg', { data: msg });
socket.emit('sendmsg', msg);
add_message("me", msg);
}
}

View File

@@ -1,5 +1,5 @@
socket.on('updatemsg', (from, data) => {
//console.log("data: " + data);
console.log("data: " + data);
add_message(from, data);
});

View File

@@ -17,17 +17,48 @@
border-radius: 5px;
}
.chat_box #msg_thread div.others {
/* * * * * * * * * * * * * *
ALL MSG
*/
.chat_box #msg_thread div {
margin-left: 0px;
background-color: rgb(210, 210, 210);
max-width: 80%;
}
.chat_box #msg_thread div p.name {
margin: 0px;
font-size: 12px;
color: rgb(100, 100, 100);
}
.chat_box #msg_thread div p.msg {
margin: 5px 0px;
}
/* * * * * * * * * * * * * *
MSG PERSO
*/
.chat_box #msg_thread div.me {
margin-right: 0px;
margin-left: auto;
background-color: rgb(210, 110, 10);
}
.chat_box #msg_thread div.me p.name {
display: none;
}
/* * * * * * * * * * * * * *
MSG SERVER
*/
.chat_box #msg_thread div.SERVER {
margin-left: auto;
background-color: transparent;
}
.chat_box #msg_thread div.SERVER p.name {
display: none;
}
@@ -36,11 +67,3 @@
font-size: 12px;
color: rgb(100, 100, 100);
}
.chat_box #msg_thread div p.name {
margin: 0px;
font-size: 12px;
color: rgb(100, 100, 100);
}
.chat_box #msg_thread div p.msg {
margin-top: 10px;
}

View File

@@ -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;

View File

@@ -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);
};

View File

@@ -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)