wip chat adding room
This commit is contained in:
@@ -75,7 +75,6 @@
|
|||||||
|
|
||||||
|
|
||||||
<!-- https://socket.io/docs/v4/client-installation/ -->
|
<!-- 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 src="https://cdn.socket.io/4.5.3/socket.io.min.js" integrity="sha384-WPFUvHkB1aHA5TDSZi6xtDgkF0wXJcIIxXhC6h8OT8EH3fC5PWro5pWJ1THjcfEi" crossorigin="anonymous"></script>
|
||||||
<script>
|
<script>
|
||||||
const socket = io("http://localhost:3000");
|
const socket = io("http://localhost:3000");
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
socket.on('connect', () => {
|
socket.on('connect', () => {
|
||||||
//socket.emit('adduser', prompt("what's your name ?"));
|
//socket.emit('adduser', prompt("what's your name ?"));
|
||||||
socket.emit('adduser', "glurk");
|
socket.emit('adduser', "glurk");
|
||||||
|
//socket.emit('joinlastroom');
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ const send_msg = () => {
|
|||||||
div_msg.focus();
|
div_msg.focus();
|
||||||
|
|
||||||
if (msg.length > 0) {
|
if (msg.length > 0) {
|
||||||
socket.emit('sendmsg', { data: msg });
|
socket.emit('sendmsg', msg);
|
||||||
add_message("me", msg);
|
add_message("me", msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
socket.on('updatemsg', (from, data) => {
|
socket.on('updatemsg', (from, data) => {
|
||||||
//console.log("data: " + data);
|
console.log("data: " + data);
|
||||||
add_message(from, data);
|
add_message(from, data);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -17,17 +17,48 @@
|
|||||||
border-radius: 5px;
|
border-radius: 5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.chat_box #msg_thread div.others {
|
|
||||||
|
/* * * * * * * * * * * * * *
|
||||||
|
ALL MSG
|
||||||
|
*/
|
||||||
|
|
||||||
|
.chat_box #msg_thread div {
|
||||||
margin-left: 0px;
|
margin-left: 0px;
|
||||||
background-color: rgb(210, 210, 210);
|
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 {
|
.chat_box #msg_thread div.me {
|
||||||
margin-right: 0px;
|
margin-right: 0px;
|
||||||
|
margin-left: auto;
|
||||||
background-color: rgb(210, 110, 10);
|
background-color: rgb(210, 110, 10);
|
||||||
}
|
}
|
||||||
.chat_box #msg_thread div.me p.name {
|
.chat_box #msg_thread div.me p.name {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* * * * * * * * * * * * * *
|
||||||
|
MSG SERVER
|
||||||
|
*/
|
||||||
|
|
||||||
|
.chat_box #msg_thread div.SERVER {
|
||||||
|
margin-left: auto;
|
||||||
|
background-color: transparent;
|
||||||
|
}
|
||||||
.chat_box #msg_thread div.SERVER p.name {
|
.chat_box #msg_thread div.SERVER p.name {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
@@ -36,11 +67,3 @@
|
|||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
color: rgb(100, 100, 100);
|
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;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -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
|
// store the username in the socket session for this client
|
||||||
socket.username = username;
|
socket.username = username;
|
||||||
// store the room name in the socket session for this client
|
// 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');
|
socket.emit('updatemsg', 'SERVER', 'you have connected to room1');
|
||||||
// echo to room 1 that a person has connected to their room
|
// 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.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;
|
module.exports = add_user;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
const send_msg = (socket, msg) => {
|
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);
|
socket.to(socket.room).emit('updatemsg', socket.username, msg);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -19,20 +19,18 @@ let rooms = ['room1', 'room2', 'room3'];
|
|||||||
|
|
||||||
io.on('connection', (socket) => {
|
io.on('connection', (socket) => {
|
||||||
|
|
||||||
console.log('a user is connected');
|
|
||||||
|
|
||||||
socket.on('adduser', (username) => {
|
socket.on('adduser', (username) => {
|
||||||
add_user(socket, username);
|
add_user(socket, username, usernames);
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('sendmsg', (msg) => {
|
socket.on('sendmsg', (msg) => {
|
||||||
send_msg(socket, msg);
|
send_msg(socket, msg);
|
||||||
});
|
});
|
||||||
|
|
||||||
// socket.on('sendmsg', function (data) {
|
socket.on('joinlastroom', () => {
|
||||||
// // we tell the client to execute 'updatechat' with 2 parameters
|
if (socket.room !== null)
|
||||||
// io.sockets.in(socket.room).emit('updatemsg', socket.username, data);
|
socket.emit('updateroom', socket.room);
|
||||||
// });
|
});
|
||||||
|
|
||||||
socket.on('switchroom', function(newroom){
|
socket.on('switchroom', function(newroom){
|
||||||
// leave the current room (stored in session)
|
// leave the current room (stored in session)
|
||||||
|
|||||||
Reference in New Issue
Block a user