-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_chat.js
More file actions
112 lines (100 loc) · 3.41 KB
/
client_chat.js
File metadata and controls
112 lines (100 loc) · 3.41 KB
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
//===============================================
// 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 msg = ($('#inputpublicchat').val() || '').trim();
if(!msg) return;//check if not id empty
let who = userid+"test";
let rng = Gun.text.random(10);
gun.get('chat').get(rng).put({
alias:who,
message:msg //enc
});
console.log("send message...");
}
});
//https://gun.eco/docs/RAD
async function InitChat(){
console.log("Init Chat...")
$('#publicchatlist').empty();
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 dec = data.message;
//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);
gunchat.map().once(qcallback);
//gunchat.map().on((data,key)=>{
//console.log(data);
//});
}
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();