Skip to content

Commit

Permalink
BE refactor and close private chat
Browse files Browse the repository at this point in the history
  • Loading branch information
adrigardi90 committed Mar 4, 2019
1 parent 623fc05 commit 612f8d7
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 12,180 deletions.
5 changes: 0 additions & 5 deletions babel.config.js

This file was deleted.

11,913 changes: 0 additions & 11,913 deletions package-lock.json

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"serve": "vue-cli-service serve",
"run:server": "node ./server/index.js",
"debug:server": "node --inspect=0.0.0.0:9229 ./server/server.js",
"debug:server": "node --inspect=0.0.0.0:9229 ./server/index.js",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
Expand Down
102 changes: 102 additions & 0 deletions server/chat_namespace/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const users = {
general: [

],
sports: [

]
};


const joinRoom = (socket, namespace) => ({ username, room }) => {
console.log(`user ${username} wants to join the room ${room}`);

// Join the room
socket.join(room, () => {
console.log(`user ${username} joined the room ${room}`);

// push user for the suitable ROOM!!!
users[room].push({ username: username })

// Notify all the users in the same room
namespace.sockets.in(room).emit('newUser', users[room]);
});

}

const publicMessage = (namespace) => ({ room, message, username }) => {
namespace.sockets.in(room).emit('newMessage', {
message,
username
});
}

const leaveRoom = (socket, namespace) => ({ room, username }) => {
console.log(`user ${username} wants to leave the room ${room}`);

socket.leave(room, () => {
console.log(`user ${username} left the room ${room}`);

let usersRoom = users[room]
// delete user from the suitable array
usersRoom = usersRoom.filter((user) => (user.username !== username))

// Notify all the users in the same room
namespace.sockets.in(room).emit('newUser', usersRoom);
})
}

const joinPrivateRoom = (socket, namespace) => ({ username, room, to }) => {
console.log(`user ${username} wants to have a private chat with ${to}`);

// Join the room
socket.join(to, () => {

if (room !== null) {
// Notify the user to talk with (in the same main room)
namespace.sockets.in(room).emit('privateChat', {
username,
to
});
}
});
}

const leavePrivateRoom = (socket, namespace) => ({ room, from, to }) => {
console.log(`user ${from} wants to leave the private chat with ${room}`);

namespace.to(room).emit('leavePrivateRoom', {
to,
privateMessage: `${from} has closed the chat`,
from,
room
})

socket.leave(room, () => {
console.log(`user ${from} left the private chat with ${room}`);
})
}

const privateMessage = (namespace) => ({ privateMessage, to, from, room }) => {
console.log(`User ${from} wants sends a message to ${to}`);

// Private message to the user
namespace.to(room).emit('privateMessage', { to, privateMessage, from, room })
}

const privateMessagePCSignaling = (namespace) => ({ desc, to, from, room }) => {
console.log(`User ${from} sends an offer ${to}`);

// Private signaling to the user
namespace.to(room).emit('privateMessagePCSignaling', { desc, to, from })
}

module.exports = {
joinRoom,
publicMessage,
leaveRoom,
joinPrivateRoom,
leavePrivateRoom,
privateMessage,
privateMessagePCSignaling
}
105 changes: 16 additions & 89 deletions server/chat_namespace/index.js
Original file line number Diff line number Diff line change
@@ -1,111 +1,38 @@
const config = require('./../config')
const events = require('./events.js')


const users = {
general: [

],
sports: [

]
};

// Socket namespace
let namespace;

onConnection = (socket) => {
// When connecting
const onConnection = (socket) => {

// Listening for joining a room
socket.on('joinRoom', ({ username, room }) => {
console.log(`user ${username} wants to join the room ${room}`);

// Join the room
socket.join(room, () => {
console.log(`user ${username} joined the room ${room}`);

// push user for the suitable ROOM!!!
users[room].push({ username: username })

// Notify all the users in the same room
namespace.sockets.in(room).emit('newUser', users[room]);
});

});
socket.on('joinRoom', events.joinRoom(socket, namespace));

// Listening for new public messages
socket.on('publicMessage', ({ room, message, username }) => {
namespace.sockets.in(room).emit('newMessage', {
message,
username
});
})
socket.on('publicMessage', events.publicMessage(namespace))

// Leave room
socket.on('leaveRoom', ({ room, username }) => {
console.log(`user ${username} wants to leave the room ${room}`);

socket.leave(room, () => {
console.log(`user ${username} left the room ${room}`);

let usersRoom = users[room]
// delete user from the suitable array
usersRoom = usersRoom.filter((user) => (user.username !== username))

// Notify all the users in the same room
namespace.sockets.in(room).emit('newUser', usersRoom);
})
})
socket.on('leaveRoom', events.leaveRoom(socket, namespace))

// Listening for private chats
socket.on('joinPrivateRoom', ({ username, room, to }) => {
console.log(`user ${username} wants to have a private chat with ${to}`);

// Join the room
socket.join(to, () => {

if (room !== null) {
// Notify the user to talk with (in the same main room)
namespace.sockets.in(room).emit('privateChat', {
username,
to
});
}
});
});
socket.on('joinPrivateRoom', events.joinPrivateRoom(socket, namespace));

// Leave private chat
socket.on('leavePrivateRoom', ({ room, from, to }) => {
console.log(`user ${from} wants to leave the private chat with ${room}`);

namespace.to(room).emit('leavePrivateRoom', {
to,
privateMessage: `${from} has closed the chat`,
from,
room
})

socket.leave(room, () => {
console.log(`user ${from} left the private chat with ${room}`);
})
})
socket.on('leavePrivateRoom', events.leavePrivateRoom(socket, namespace))

// Private message listener
socket.on('privateMessage', ({ privateMessage, to, from, room }) => {
console.log(`User ${from} wants sends a message to ${to}`);

// Private message to the user
namespace.to(room).emit('privateMessage', { to, privateMessage, from, room })
})
socket.on('privateMessage', events.privateMessage(namespace))

// Private message for Signaling PeerConnection
socket.on('privateMessagePCSignaling', ({ desc, to, from, room }) => {
console.log(`User ${from} sends an offer ${to}`);

// Private signaling to the user
namespace.to(room).emit('privateMessagePCSignaling', { desc, to, from })
})
socket.on('privateMessagePCSignaling', events.privateMessagePCSignaling(namespace))

}

exports.createNameSpace = (io) => {
namespace = io
namespace.on('connection', onConnection)
namespace = io
namespace
// .of(config.CHAT_NAMESPACE)
.on('connection', onConnection)
}
3 changes: 2 additions & 1 deletion server/config/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

const CONFIG = {
PORT: 3000 || process.env.PORT
PORT: 3000 || process.env.PORT,
CHAT_NAMESPACE: '/chat'
}

module.exports = CONFIG
1 change: 0 additions & 1 deletion server/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

const app = require('./app')
const http = require('http');
const config = require('./config')
Expand Down
124 changes: 0 additions & 124 deletions server/server.js

This file was deleted.

Loading

0 comments on commit 612f8d7

Please sign in to comment.