-
Notifications
You must be signed in to change notification settings - Fork 237
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
Daniel Ribeiro
committed
Apr 3, 2019
1 parent
0825ecf
commit d8dd46c
Showing
3 changed files
with
82 additions
and
64 deletions.
There are no files selected for viewing
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,29 +1,29 @@ | ||
var HashMap = require('hashmap'); | ||
// Constructor | ||
function OfficeController() { | ||
this.usersInRoomOffice = new HashMap(); | ||
this.usersInRoomOffice = new HashMap(); | ||
} | ||
|
||
OfficeController.prototype.addUserInRoom = function(user,room) { | ||
this.removeUser(user.id) | ||
OfficeController.prototype.addUserInRoom = function (user, room) { | ||
this.removeUser(user.id) | ||
var userInRoom = { | ||
user:user, | ||
room:room | ||
}; | ||
user: user, | ||
room: room | ||
}; | ||
this.usersInRoomOffice.set(user.id, | ||
userInRoom | ||
userInRoom | ||
); | ||
|
||
return userInRoom; | ||
}; | ||
|
||
OfficeController.prototype.removeUser = function(userId) { | ||
OfficeController.prototype.removeUser = function (userId) { | ||
this.usersInRoomOffice.delete(userId); | ||
}; | ||
|
||
OfficeController.prototype.getUsersInOffice = function() { | ||
OfficeController.prototype.getUsersInOffice = function () { | ||
return this.usersInRoomOffice; | ||
}; | ||
|
||
|
||
module.exports = OfficeController; | ||
module.exports = OfficeController; |
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,54 +1,52 @@ | ||
var OfficeController = require('./controllers/office.controller'); | ||
var OfficeController = require('./controllers/office.controller'); | ||
|
||
// Constructor | ||
function Office(server) { | ||
this.officeController = new OfficeController(); | ||
|
||
const io = require("socket.io")(server) | ||
|
||
io.use(function(socket, next){ | ||
|
||
io.use(function (socket, next) { | ||
socket.user = JSON.parse(socket.handshake.query.user) | ||
return next(); | ||
|
||
return next(); | ||
}); | ||
|
||
io.on('connection', (socket) => { | ||
console.log('New user connected') | ||
var that = this; | ||
var currentUser = socket.user; | ||
var currentUser = socket.user; | ||
|
||
|
||
|
||
var room_param = socket.handshake.query.room; | ||
var room = room_param ? room_param : "room-1"; | ||
addUserInRoom(socket.user, room); | ||
|
||
that.officeController.getUsersInOffice().forEach(function(value, key) { | ||
addUserInRoom(value.user,value.room); | ||
that.officeController.getUsersInOffice().forEach(function (value, key) { | ||
addUserInRoom(value.user, value.room); | ||
}); | ||
|
||
|
||
socket.on('disconnect', function(socket) { | ||
console.log("disconnect"); | ||
io.sockets.emit('disconnect', currentUser.id); | ||
that.officeController.removeUser(currentUser.id) | ||
socket.on('disconnect', function (socket) { | ||
io.sockets.emit('disconnect', currentUser.id); | ||
that.officeController.removeUser(currentUser.id) | ||
|
||
}); | ||
|
||
socket.on('enter-room', (data) => { | ||
addUserInRoom(currentUser,data.room); | ||
}) | ||
addUserInRoom(currentUser, data.room); | ||
}) | ||
|
||
function addUserInRoom(user,room){ | ||
//console.log(user,room) | ||
var userInRoom = that.officeController.addUserInRoom(user,room) | ||
io.sockets.emit('enter-room', userInRoom); | ||
} | ||
function addUserInRoom(user, room) { | ||
//console.log(user,room) | ||
var userInRoom = that.officeController.addUserInRoom(user, room) | ||
|
||
io.sockets.emit('enter-room', userInRoom); | ||
} | ||
|
||
}); | ||
|
||
|
||
} | ||
|
||
module.exports = Office; | ||
module.exports = Office; |
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,73 +1,93 @@ | ||
$(function(){ | ||
$(function () { | ||
|
||
var enterRoom = $("[enter-room]") | ||
|
||
var matrixProfile = new MatrixProfile() | ||
|
||
if (matrixProfile.isProfileStored()) { | ||
enterInOffice(matrixProfile); | ||
}else{ | ||
} else { | ||
redirectToHome(); | ||
} | ||
|
||
function removeUser(userId){ | ||
$('#'+userId).remove(); | ||
function removeUser(data, userId) { | ||
notify(data, `${data.user.name} saiu da sala`) | ||
$('#' + userId).remove(); | ||
} | ||
|
||
function showUserInRoom(user,room){ | ||
|
||
var userView = $('#'+user.id).length; | ||
if(userView==0){ | ||
userView = $('<img width="50px" id="'+user.id+'"src="'+user.imageUrl+'">'); | ||
}else{ | ||
userView = $('#'+user.id).detach(); | ||
} | ||
function showUserInRoom(user, room) { | ||
|
||
$("#"+room).append(userView); | ||
var userView = $('#' + user.id).length; | ||
if (userView == 0) { | ||
userView = $('<img width="50px" id="' + user.id + '"src="' + user.imageUrl + '">'); | ||
} else { | ||
userView = $('#' + user.id).detach(); | ||
} | ||
|
||
$("#" + room).append(userView); | ||
} | ||
|
||
function redirectToHome(){ | ||
function redirectToHome() { | ||
window.location.href = "./" | ||
} | ||
|
||
function goToMeet(externalMeetUrl){ | ||
function goToMeet(externalMeetUrl) { | ||
var r = confirm("Deseja entrar na call?"); | ||
if (r == true) { | ||
window.open(externalMeetUrl, '_blank'); | ||
window.open(externalMeetUrl, '_blank'); | ||
} else { | ||
txt = "You pressed Cancel!"; | ||
txt = "You pressed Cancel!"; | ||
} | ||
} | ||
|
||
function saveLastRoom(data) { | ||
localStorage.setItem('last_room' + data.user.id, data.room); | ||
} | ||
|
||
function enterInOffice(matrixProfile){ | ||
function notify(data, title) { | ||
var options = { | ||
icon: data.user.imageUrl, | ||
} | ||
|
||
if (Notification.permission !== "granted") { | ||
Notification.requestPermission(); | ||
} else { | ||
var n = new Notification(title, options); | ||
} | ||
} | ||
|
||
function enterInOffice(matrixProfile) { | ||
var lastRoom = localStorage.getItem('last_room' + matrixProfile.loadStoredProfile().id); | ||
console.log(window.location) | ||
//make connection | ||
var socket = io.connect(window.location.protocol + "//" + window.location.host, { | ||
|
||
//make connection | ||
var socket = io.connect(window.location.protocol + "//" + window.location.host, { | ||
query: "user=" + matrixProfile.loadStoredProfileAsString() + (lastRoom ? "&room=" + lastRoom : "") | ||
}) | ||
|
||
enterRoom.on("click",function(e){ | ||
enterRoom.on("click", function (e) { | ||
var room = $(e.target).parent().attr("id"); | ||
socket.emit('enter-room', {room : room,user:matrixProfile.loadStoredProfile()}) | ||
socket.emit('enter-room', { room: room, user: matrixProfile.loadStoredProfile() }) | ||
setTimeout(function () { | ||
goToMeet($(e.target).attr("external-meet-url")); | ||
},300); | ||
}, 300); | ||
|
||
}) | ||
|
||
socket.on("enter-room", (data) => { | ||
saveLastRoom(data); | ||
showUserInRoom(data.user,data.room); | ||
var loggedUserId = JSON.parse(localStorage.getItem('user')).id; | ||
var loggedUserRoomId = localStorage.getItem('last_room' + data.user.id); | ||
|
||
if (loggedUserRoomId == data.room && loggedUserId != data.user.id) { | ||
notify(data, `${data.user.name} entrou na sala`) | ||
} | ||
|
||
saveLastRoom(data) | ||
showUserInRoom(data.user, data.room) | ||
}) | ||
|
||
socket.on("disconnect", (userId) => { | ||
removeUser(userId); | ||
socket.on("disconnect", (data) => { | ||
removeUser(data, userId); | ||
}) | ||
} | ||
|
||
}); | ||
}); |