Long pulling method added

This commit is contained in:
Eudes Inácio
2021-07-26 10:52:14 +01:00
parent 1c25190787
commit 21c0bd8956
2 changed files with 66 additions and 2 deletions
+37
View File
@@ -23,6 +23,7 @@ export class ChatService {
SERVER_URL = 'wss://www.tabularium.pt/websocket';
public messages: Subject<any>;
loggedUserChat:any;
bindOnMessage: any;
constructor(
private http:HttpClient,
@@ -242,4 +243,40 @@ export class ChatService {
return this.http.post(environment.apiChatUrl+'groups.kick', body, opts);
}
async subscribe(roomId:any) {
console.log('Subcrive')
let params = new HttpParams();
params = params.set("roomId", roomId);
let opts = {
headers: this.headers,
params: params
}
this.http.get(environment.apiChatUrl+'im.messages', opts).subscribe(async res => {
console.log("Subcrive", res)
if (res == 502) {
// Connection timeout
// happens when the connection was pending for too long
// let's reconnect
await this.subscribe(roomId);
} else if (res != 200) {
// Show Error
//showMessage(response.statusText);
this.getRoomMessages(roomId)
// Reconnect in one second
await new Promise(resolve => setTimeout(resolve, 1000));
await this.subscribe(roomId);
} else {
// Got message
//let message = await response.text();
this.getRoomMessages(roomId)
await this.subscribe(roomId);
}
})
}
}
+29 -2
View File
@@ -59,6 +59,7 @@ export class MessagesPage implements OnInit, AfterViewChecked, OnChanges {
this.load();
/* }, 9000); */
console.log(this.roomId);
}
@@ -113,14 +114,19 @@ export class MessagesPage implements OnInit, AfterViewChecked, OnChanges {
}
loadMessages(){
this.showLoader = true;
//this.showLoader = true;
this.chatService.getRoomMessages(this.roomId).subscribe(res => {
/* console.log(res); */
this.messages = res['messages'].reverse();
console.log(this.messages);
this.showLoader = false;
this.serverLongPull(res)
/* this.chatService.subscribe(this.roomId).then(res => {
console.log("Real fake msg", res)
}); */
//this.showLoader = false;
})
}
getChatMembers(){
console.log(this.roomId);
@@ -282,6 +288,27 @@ export class MessagesPage implements OnInit, AfterViewChecked, OnChanges {
return await modal.present();
}
async serverLongPull(res){
if (res == 502) {
// Connection timeout
// happens when the connection was pending for too long
// let's reconnect
await this.serverLongPull(res);
} else if (res != 200) {
// Show Error
//showMessage(response.statusText);
this.load()
// Reconnect in one second
await new Promise(resolve => setTimeout(resolve, 1000));
await this.serverLongPull(res);
} else {
// Got message
//let message = await response.text();
this.load()
await this.serverLongPull(res);
}
}
}