diff --git a/src/app/home/home.page.ts b/src/app/home/home.page.ts index 974b5aef7..c76e46650 100644 --- a/src/app/home/home.page.ts +++ b/src/app/home/home.page.ts @@ -95,45 +95,29 @@ export class HomePage implements OnInit { private RouteService: RouteService, private RocketChatClientService: RocketChatClientService) { - this.RocketChatClientService.connect(()=>{ + this.RocketChatClientService.getRooms().then((rooms: any)=>{ + console.log('rooms', rooms) - this.RocketChatClientService.login({ - username: 'paulo.pinto', - email: 'paulo.pinto@gabinetedigital.local', - password: 'tabteste@006' - }).then((message) => { - // alert('login') - console.log('rocket chat login successfully', message) - this.RocketChatClientService.getRooms().then((rooms: any)=>{ - // console.log('rooms', rooms) - - rooms.result.update.forEach((room:any) => { - // console.log('room', room) - this.RocketChatClientService.subscribe(room.lastMessage.rid).then((subscription)=>{ - console.log('subscription', subscription) - }) - }); + rooms.result.update.forEach((room:any) => { + console.log('room', room) + this.RocketChatClientService.subscribe(room.lastMessage.rid).then((subscription)=>{ + 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.receiveLiveMessageFromRoom( + 'fsMwcNdufWvdnChj7ya9nF9cX2HizxxWAM', + this.constructor.name, + (Chatmessage)=>{ + console.log('chat', Chatmessage) } + ) - }).catch((message)=>{ - console.log('rocket chat login failed', message) - }).finally(()=>{ - }) - - }) + window['jj'] = ()=>{ + //send message // roomId // Message + this.RocketChatClientService.send('fsMwcNdufWvdnChj7ya9nF9cX2HizxxWAM', 'Mensagem enviada programaticamente.'+ new Date().toISOString()) + } // this.RocketChatClientService.send() /* this.webNotificationPopupService.askNotificationPermission() */ diff --git a/src/app/models/user.model.ts b/src/app/models/user.model.ts index 52b214313..e0e4984dc 100644 --- a/src/app/models/user.model.ts +++ b/src/app/models/user.model.ts @@ -56,6 +56,7 @@ export class UserSession { TypeShare: number; }[] UserName: string + Password: string Profile: any; LoginPreference: 'None' | 'Password' | 'Pin' | null; PIN: string diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index 3024f8bff..c8ba0a3fb 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -30,12 +30,25 @@ export class AuthService { public alertController: AlertController, private aesencrypt: AESEncrypt, private cookieService: CookieService, + private RocketChatClientService: RocketChatClientService ) { this.headers = new HttpHeaders(); if (SessionStore.exist) { this.ValidatedUser = SessionStore.user + + this.RocketChatClientService.connect(()=>{ + this.RocketChatClientService.login({ + username: SessionStore.user.UserName, + password: SessionStore.user.Password + }).then((message) => { + console.log('rocket chat login successfully', message) + }).catch((message)=>{ + console.log('rocket chat login failed', message) + }) + }) + } if (localStorage.getItem("userChat") != null) { @@ -77,6 +90,8 @@ export class AuthService { session.Profile = 'MDGPR' } + session.Password = user.password + session.BasicAuthKey = user.BasicAuthKey SessionStore.reset(session) @@ -98,6 +113,22 @@ export class AuthService { "password": user.password, } + + this.RocketChatClientService.connect(()=>{ + + this.RocketChatClientService.login({ + username: user.username, + password: user.password + }).then((message) => { + console.log('rocket chat login successfully', message) + }).catch((message)=>{ + console.log('rocket chat login failed', message) + }).finally(()=>{ + }) + + }) + + let responseChat = await this.httpService.post('login', postData).toPromise(); if(responseChat) { console.log('Login to Rocket chat OK'); diff --git a/src/app/services/socket/rocket-chat-client.service.ts b/src/app/services/socket/rocket-chat-client.service.ts index 85a13a33c..89d27dc25 100644 --- a/src/app/services/socket/rocket-chat-client.service.ts +++ b/src/app/services/socket/rocket-chat-client.service.ts @@ -19,23 +19,34 @@ export class RocketChatClientService { private returns() {} - private attemptRequestQueue(arg, option) { + private attemptRequestQueue(arg, options) { - const conditionToSend = this.ws.connected == true && this.isLogin == true + const conditionToSend = this.ws.connected == false || this.isLogin == false - if(conditionToSend) { + if(!conditionToSend) { + console.log('requestQueue') this.requestQueue.push({ arg: arg, + methodName: options.methodName, status: { isLogin: this.isLogin, isConnected: this.ws.connected } }) } + return conditionToSend } - private runRequestQueue(){} + private runRequestQueue(){ + this.requestQueue.forEach((request,index, object)=>{ + console.log('runRequestQueue '+index) + const method = request.methodName + const arg = request.arg + this[method](...arg) + object.splice(index, 1); + }) + } connect(firstPingFunx: Function) { this.hasPing = false @@ -91,8 +102,8 @@ export class RocketChatClientService { if(message.result) { if(message.result.token) { this.isLogin = true - this.runRequestQueue() this.ws.wsMsgQueue() + this.runRequestQueue() resolve(message) } else { this.isLogin = false @@ -342,17 +353,14 @@ export class RocketChatClientService { onopen:()=> { this.ws.connected = true console.log('================== welcome to socket server =====================') + this.ws.wsMsgQueue() + }, + wsMsgQueue:()=> { this.wsMsgQueue.forEach((item, index, object) => { this.ws.send(item.message, item.requestId); object.splice(index, 1); }) }, - wsMsgQueue:()=> { - // this.wsMsgQueue.forEach((item, index, object) => { - // this.ws.send(item.message, item.requestId); - // object.splice(index, 1); - // }) - }, send: (message: object, requestId = uuidv4()) => { if (this.ws.connected == false) { // save data to send when back online