forked from adrigardi90/video-chat
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
623fc05
commit 612f8d7
Showing
10 changed files
with
164 additions
and
12,180 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.