85 lines
1.7 KiB
JavaScript
85 lines
1.7 KiB
JavaScript
class Notice {
|
||
constructor(client) {
|
||
this.client = client;
|
||
this.connect();
|
||
|
||
}
|
||
|
||
async connect() {
|
||
let url = `${window.location.protocol[4] === 's' ? 'wss' : 'ws'}://${window.location.host}/ws/chat/notice`;
|
||
|
||
this.chatSocket = new WebSocket(url);
|
||
this.chatSocket.onmessage = (event) =>{
|
||
let data = JSON.parse(event.data);
|
||
//console.log("notice: ", data);
|
||
if (data.type == "invite")
|
||
this.receiveInvite(data);
|
||
else if (data.type == "online_users" || data.type == "disconnect")
|
||
this.receiveOnlineUser(data);
|
||
}
|
||
this.chatSocket.onopen = (event) => {
|
||
this.online_users = {};
|
||
this.getOnlineUser();
|
||
}
|
||
}
|
||
|
||
async disconnect() {
|
||
if (this.chatSocket == undefined)
|
||
this.chatSocket.close();
|
||
|
||
this.chatSocket.send(JSON.stringify({
|
||
type: "online_users",
|
||
targets: "all",
|
||
}));
|
||
}
|
||
|
||
async sendInvite(id_inviter, id_inviteds) {
|
||
|
||
if (this.chatSocket == undefined)
|
||
return;
|
||
|
||
this.chatSocket.send(JSON.stringify({
|
||
type: "invite",
|
||
targets: id_inviteds,
|
||
}));
|
||
|
||
}
|
||
|
||
async receiveInvite(data) {
|
||
|
||
if (data.content === "notice return") {
|
||
if (data.status == 200)
|
||
return
|
||
// Notification pour dire que la notif a été bien envoyé
|
||
else if (data.status == 404)
|
||
return
|
||
// Pas connecté
|
||
}
|
||
else {
|
||
// Géré la reception de l'invitation
|
||
}
|
||
}
|
||
|
||
async getOnlineUser() {
|
||
|
||
if (this.chatSocket == undefined)
|
||
return;
|
||
|
||
this.chatSocket.send(JSON.stringify({
|
||
type: "online_users",
|
||
targets: "all",
|
||
}));
|
||
|
||
}
|
||
|
||
async receiveOnlineUser(data) {
|
||
if (data.content !== undefined) {
|
||
this.online_users = data.content;
|
||
if (this.rewrite_usernames !== undefined)
|
||
this.rewrite_usernames();
|
||
}
|
||
}
|
||
}
|
||
|
||
export {Notice}
|