socket = io('https://socket.trueoriginal.com',{
path:'/socket.io/',
autoConnect:true,
auth: {
token: settings['redis_token'],
uid: settings['redis_uid'],
site_id: settings['site_id']
}
});
socket.on('connect', () => {
vvDebugg('++--> Conntected to socket');
setPollTimer(1000,'socket');
for(i in _duration){
socket.emit('loadtime',{site_id:settings.site_id,time:_duration[i],page:i});
delete _duration[i];
}
});
socket.on('disconnect', () => {
vvDebugg('***--> Disconnected from socket');
});
//Events
socket.on('event', function(data) {
console.log('---------------------------------------------------------------------------------------- EVENT');
console.log(data);
//Send message
if(data.action == 'message'){
alert(data.message);
}
//call socketEvent
$(document).trigger('socketEvent',data);
//Customer things
if(settings['site_id'] == data?.parms?.site_id){
for(x in data.parms.funtions){
var f = data.parms.funtions[x];
//Run pipe
if(f.funtion == 'vvPipe'){
vvPipe(f.args);
}
}
if(data.operation == "notification"){
pollData(true);
}
//Same table
if(data.parms.table == settings['redis_db']){
console.log('- EVENT :: same table');
console.log('- EVENT :: operation ['+data.operation+']');
console.log('- EVENT :: id ['+data.parms.id+']');
//Delete item
if(data.operation == 'delete'){
$("#item-"+data.parms.id).remove();
}
if(data.operation == 'create' || data.operation == 'update' || data.operation == 'update' || data.operation == 'delete'){
$("#pending-update").show();
if(data.parms.id == settings['redis_id']){
for(x in vvDosubmits){
if(vvDosubmits[x] == settings['redis_key']){
console.log('- EVENT :: IS IN vvDosubmits --------------------');
delete vvDosubmits[x];
return 2;
}
}
console.log('- EVENT :: IS NOT IN vvDosubmits --------------------');
new swal({
title: "This post was recently updated",
text:'Reload the page to get the most recent information.',
icon: "info",
buttons: {
cancel: true,
remove: {
text: "Reload",
value: true,
}
}
}).then((value) => {
if(value){
window.location.reload();
}else{
return false;
}
});
}
}
}
}
//Change site
if(data.action == 'change-site'){
if(_vvChangingSite){
return;
}
//Reload site
//$("#changeSiteId").val(data.site_id).trigger('change');
//Change title
/*
$("#changeSiteId").val(data.site_id).select2('');
$("#current-site-title").html($("#changeSiteId option:selected" ).text());
settings['site_id'] = data.site_id;
*/
//$('#page-header----sites').tooltip(['title':data.message2]).tooltip('show');
//$('#page-header----sites').tooltip({customClass:'red-tooltip',template:'
'}).tooltip('hide').attr('data-original-title', data.message).tooltip('update').tooltip('show');
new swal({
title: "You have changed site in another tab",
text:'Reload the page to get the most recent information.',
icon: "info",
buttons: {
cancel: true,
remove: {
text: "Reload",
value: true,
}
}
}).then((value) => {
if(value){
window.location.reload();
}else{
$("#changeSiteId").parents('.dropdown-menu').parent().append(' !
');
$("#changeSiteId").val(data.site_id).select2('');
$("#current-site-title").html($("#changeSiteId option:selected" ).text());
settings['site_id'] = data.site_id;
return false;
}
});
}
//Magic link
if(data.action == 'magiclink' && _data != undefined){
for(i in _data.codes){
code = _data.codes[i];
if(code == data.code){
top.location = '/?init=authCheckCode&code='+code;
}
}
}
//Sessions terminated
if(data.action == 'sign-out' && data != undefined){
if(data.user_id == settings['uid']){
top.location.reload();
}
}
});
socket.on('message', function(msg) {
const patterns = {
emails: /emails-[a-zA-Z0-9]+\b/,
automatic: /automatic-\d+/,
myhRobot: new RegExp("myh-robot"),
robotMyh: new RegExp("robot_myh"),
crtIssued: new RegExp("crt_issued"),
notifications: new RegExp("notifications"),
sites: new RegExp("sites--"+settings['site_id']),
tasks:/log(?=::)/,
}
for (const [key, pattern] of Object.entries(patterns)) {
let matches = msg.match(pattern);
if(matches){
if (key === 'automatic' && msg.match(/start/)) {
vvGetAutomaticData();
}
if (key === 'emails') {
//vvGetEmailsProgressData();
}
$(document).trigger(matches[0],msg);
}
}
let swalAlert = function() {
new swal({
title: "This post was recently updated",
text:'Reload the page to get the most recent information.',
icon: "info",
buttons: {
cancel: true,
remove: {
text: "Reload",
value: true,
}
}
}).then((value) => {
if(value){
window.location.reload();
}else{
return false;
}
});
}
if(settings['redis_key'] != undefined){
let redisKeyParts1 = settings['redis_key'].split('----');
let redisPattern1 = new RegExp(`${redisKeyParts1[0]}----${redisKeyParts1[1]}`);
if(redisPattern1.test(msg)){
//swalAlert();
return;
}
let redisKeyParts = settings['redis_key'].split('--');
//like database--site_id--id
let redisPattern2 = new RegExp(`\\b${redisKeyParts[0]}--${redisKeyParts[1]}--${redisKeyParts[2]}\\b`);
if(redisPattern2.test(msg)){
//swalAlert();
return;
}
//like database--site_id
let redisPattern3 = new RegExp(`${redisKeyParts[0]}--${redisKeyParts[1]}`);
if(redisPattern3.test(msg)){
var _key = redisKeyParts[0]+'--'+redisKeyParts[1];
setPollTimer(1000,_key);
}
}
});