diff --git a/src/app/home/home.page.ts b/src/app/home/home.page.ts index 696add888..823028df0 100644 --- a/src/app/home/home.page.ts +++ b/src/app/home/home.page.ts @@ -104,7 +104,7 @@ export class HomePage implements OnInit { username: 'paulo.pinto', email: 'paulo.pinto@gabinetedigital.local', password: 'tabteste@006' - }).then((message)=>{ + }).then((message) => { // alert('login') console.log('rocket chat login successfully', message) this.RocketChatClientService.getRooms().then((rooms: any)=>{ @@ -116,17 +116,21 @@ export class HomePage implements OnInit { console.log('subscription', subscription) }) }); + }); + this.RocketChatClientService.receiveLiveMessageFromRoom( + 'fsMwcNdufWvdnChj7ya9nF9cX2HizxxWAM', + this.constructor.name, + (Chatmessage)=>{ + console.log('chat', Chatmessage) + } + ) - }) + window['jj'] = ()=>{ + //send message // roomId // Message + this.RocketChatClientService.send('fsMwcNdufWvdnChj7ya9nF9cX2HizxxWAM', 'Mensagem enviada programaticamente.'+ new Date().toISOString()) + } - - this.RocketChatClientService.subscribe('fsMwcNdufWvdnChj7ya9nF9cX2HizxxWAM').then((subscription)=>{ - console.log(subscription) - }) - - //send message // roomId // Message - this.RocketChatClientService.send('fsMwcNdufWvdnChj7ya9nF9cX2HizxxWAM', 'Mensagem enviada programaticamente.'+ new Date().toISOString()) }).catch((message)=>{ console.log('rocket chat login failed', message) }).finally(()=>{ diff --git a/src/app/services/socket/rocket-chat-client.service.ts b/src/app/services/socket/rocket-chat-client.service.ts index a79172adb..a3b0ebfa7 100644 --- a/src/app/services/socket/rocket-chat-client.service.ts +++ b/src/app/services/socket/rocket-chat-client.service.ts @@ -193,6 +193,57 @@ class _RocketChatClientService { } + /** + * @param roomId + * @param key + * @param funx + */ + receiveLiveMessageFromRoom(roomId, key, funx: Function) { + + this.ws.registerCallback({ + type:'Onmessage', + requestId: key, + funx:(message)=>{ + if(message.msg =='result') { + if(message.result.msg) { + if(message.result.rid == roomId) { + funx(message) + } + } + } + } + }) + + } + + streamRoomMessages(roomId : string) { + + const conditions = this.attemptRequestQueue({roomId},{methodName: 'subscribe'}) + if(conditions == false) { return new Promise((resolve, reject) => { reject('no error')}); } + + const requestId = uuidv4() + + const request = { + "msg": "sub", + "id": requestId, + "name": "stream-room-messages", + "params":[ + `${roomId}/event`, + false + ] + } + + this.ws.send(request); + + return new Promise((resolve, reject) => { + this.ws.registerCallback({type:'Onmessage', requestId, runOnces: true, funx:(message)=>{ + if(message.msg == 'ready' || message.subs[0] == requestId) { // same request send + resolve(message) + } + }}) + }); + } + streamNotifyRoom(roomId : string) { const conditions = this.attemptRequestQueue({roomId},{methodName: 'subscribe'}) if(conditions == false) { return new Promise((resolve, reject) => { reject('no error')}); } @@ -221,10 +272,6 @@ class _RocketChatClientService { }); } - - private disconnect = () => { - - } // socket class ================================================================== private socket!: WebSocket;