|
Server : Apache/2.4.41 (Ubuntu) System : Linux vmi1525618.contaboserver.net 5.4.0-105-generic #119-Ubuntu SMP Mon Mar 7 18:49:24 UTC 2022 x86_64 User : www-data ( 33) PHP Version : 8.2.12 Disable Function : NONE Directory : /var/www/nikkilgupta.edukrypt.in/public/chat/ |
Upload File : |
$(document).ready(function () {
var chatID = $('.chatid').val();
var user_id;
var displaytitle;
var isLogin = false;
// Play Sound
function playSound() {
var path = _base_url + "assets/chat/noti.mp3";
var mp3Source = '<source src="' + path + '" type="audio/mpeg">';
var embedSource = '<embed hidden="true" autostart="true" loop="false" src="' + path + '">';
document.getElementById("sound").innerHTML = '<audio autoplay="autoplay">' + mp3Source + embedSource + '</audio>';
}
function getdatetime() {
var currentdate = new Date();
var datetime = "" + currentdate.getDate() + "/"
+ (currentdate.getMonth() + 1) + "/"
+ currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
return datetime;
}
// Firebase Config
var config = {
apiKey: "AIzaSyAr__YzEbRf8EKVE_OJYpcWlBIshi4xdmE",
authDomain: "mahendraschat.firebaseapp.com",
databaseURL: "https://mahendraschat.firebaseio.com",
projectId: "mahendraschat",
storageBucket: "mahendraschat.appspot.com",
messagingSenderId: "27777970740",
appId: "1:27777970740:web:6964b91ccddd041d"
};
firebase.initializeApp(config);
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
$('#login-box').hide();
displaytitle = user.displayName;
user_id = user.email;
isLogin = true;
}
else {
$('#login-box').show();
}
});
$('#join-btn').click(function () {
var email1 = "email" + $('#usrfemail').val() + "@gmail.com";
var userName = $('#usrfemail').val();
var password1 = "123456";
firebase.auth().signInWithEmailAndPassword(email1, password1).then(function () {
var user = firebase.auth().currentUser;
user.updateProfile({
displayName: "" + userName + "",
}).then(function () {
location.reload(true);
}).catch(function (error) {
// An error happened.
});
}).catch(function (error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode == "auth/invalid-email") {
$('#err-email').html("Invalid Email..");
}
if (errorCode == "auth/user-not-found") {
firebase.auth().createUserWithEmailAndPassword(email1, password1).then(function () {
var user = firebase.auth().currentUser;
user.updateProfile({
displayName: "" + userName + "",
}).then(function () {
location.reload(true);
}).catch(function (error) {
// An error happened.
});
}).catch(function (error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
$('#err-msg').html(errorMessage);
});
}
});
});
// get firebase database reference...
var db_ref = firebase.database().ref('/' + chatID);
db_ref.on('child_added', function (data) {
if (isLogin) {
var type;
if (data.val().user_id == displaytitle) {
type = "self";
}
else {
type = "other";
}
$('<li class="' + type + '"> <div class="msg"> <p class="chatmsg">' + data.val().message + '</p> <p class="chatusr"><i>' + data.val().user_id + '</i></p> <time><i class="fa fa-clock-o"></i> ' + data.val().datetime + '</time> </div> </li>').appendTo($('.chat'));
$(document).scrollTop($(document).height());
}
});
// Delete history
db_ref.on('child_removed', function (data) {
location.reload(true);
});
// Firebase Signout
$('.signout').click(function () {
firebase.auth().signOut().then(function () {
location.reload(true);
}, function (error) {
//console.error('Sign Out Error', error);
});
});
// Write into DB
function writeUserData(message) {
db_ref.push({
user_id: displaytitle,
message: message,
datetime: getdatetime()
});
$('.message-input').val(null);
}
function newMessage() {
message = $(".message-input").val();
if ($.trim(message) == '') {
return false;
}
writeUserData(message);
};
$('.submit').click(function () {
newMessage();
});
$(window).on('keydown', function (e) {
if (e.which == 13) {
newMessage();
return false;
}
});
// Online Users
var listRef = firebase.database().ref("/presence" + chatID);
var userRef = listRef.push();
// Add ourselves to presence list when online.
var presenceRef = firebase.database().ref("/.info/connected");
presenceRef.on("value", function (snap) {
if (snap.val()) {
// Remove ourselves when we disconnect.
userRef.onDisconnect().remove();
userRef.set(true);
}
});
// Number of online users is the number of objects in the presence list.
// listRef.on("value", function (snap) {
// $('.onlineusr').html("Live: " + snap.numChildren());
// });
});