-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_chat_RAD.js
122 lines (108 loc) · 3.87 KB
/
client_chat_RAD.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//===============================================
// CLEAR GUN DATABASE
localStorage.clear();
// INIT GUN DATABASE
let gunurl = window.location.origin+'/gun';
//console.log(gunurl);
var gun = Gun(gunurl);
gun.on('hi', peer => {//peer connect
console.log('connect peer to',peer);
//console.log('peer connect!');
});
gun.on('bye', (peer)=>{// peer disconnect
console.log('disconnected from', peer);
//console.log('disconnected from peer!');
});
//===============================================
// PUBLIC CHAT
//===============================================
var gunchat;
function timestamp(){
let currentDate = new Date();
//console.log(currentDate);
let year = currentDate.getFullYear();
let month = ("0" + (currentDate.getMonth() + 1 ) ).slice(-2);
let date = ("0" +currentDate.getDate()).slice(-2);
let hour = ("0" +currentDate.getHours()).slice(-2);
let minute = ("0" +currentDate.getMinutes()).slice(-2);
let second = ("0" +currentDate.getSeconds()).slice(-2);
let millisecond = currentDate.getMilliseconds();
return year + "/" + (month) + "/" + date + ":" + hour+ ":" + minute+ ":" + second+ ":" + millisecond;
}
function scrollPublicMessage(){
let element = document.getElementById("publicchatlist");
element.scrollTop = element.scrollHeight;
}
var userid = Gun.text.random(10);
$("#inputpublicchat").keyup(async function(e) {
if(e.key == "Enter"){
console.log("Enter");
//let user = gun.user();
//if(!user.is){ return }//check if user exist
let msg = ($('#inputpublicchat').val() || '').trim();
if(!msg) return;//check if not id empty
let encmsg = await SEA.work("public","chat");//encrypttion key default?
//console.log(encmsg);
let enc = await SEA.encrypt(msg,encmsg);
//console.log(enc);
//let who = await user.get('alias').then();
let who = userid+"test";
//console.log(who);
//console.log(typeof enc)
enc = window.btoa(enc);
gun.get('chat').get(timestamp()).put({
alias:who,
message:enc
});
console.log("send message...");
}
});
//https://gun.eco/docs/RAD
async function InitChat(){
console.log("Init Chat...")
$('#publicchatlist').empty();
let encmsg = await SEA.work("public","chat"); //encrypttion key default?
async function qcallback(data,key){
console.log('incoming messages...')
//console.log("key",key);
console.log("data",data);
if(data == null)return;
if(data.message != null){
let message = window.atob(data.message);
//console.log(message);
let dec = await SEA.decrypt(message,encmsg);
//console.log(dec)
if(dec!=null){
$('#publicchatlist').append($('<div/>', {
id: key,
text : data.alias + ": " + dec
}));
scrollPublicMessage();
}
}
}
let currentDate = new Date();
let year = currentDate.getFullYear();
let month = ("0" + (currentDate.getMonth() + 1 ) ).slice(-2);
let date = ("0" +currentDate.getDate()).slice(-2);
let timestring = year + "/" + month + "/" + date + ":";
console.log(timestring);
if(gunchat !=null){
gunchat.off()
}
gunchat = gun.get('chat');
//gunchat.get({'.': {'*': '2019/08/'}}).map().once(qcallback);
//gunchat.get({'.': {'*': timestring}}).map().once(qcallback);
gunchat.get({'.': {'*': timestring},'%': 50000}).map().once(qcallback);
}
function PublicChatResize(){
let height = $(window).height(); - $('#publicchat').offset().top;
$('#publicchat').css('height', height - 50);
height = $('#publicchat').height();
$('#publicchatlist').css('height', height - 44);
}
$(window).resize(function() {
PublicChatResize();
});
InitChat();
PublicChatResize();