Merge branch 'feature/websocket' of https://bitbucket.org/equilibriumito/gabinete-digital into feature/websocket

This commit is contained in:
tiago.kayaya
2022-01-11 12:34:49 +01:00
15 changed files with 424 additions and 116 deletions
+33 -2
View File
@@ -30,13 +30,25 @@ export class AuthService {
public alertController: AlertController,
private aesencrypt: AESEncrypt,
private cookieService: CookieService,
//private realtimeChat: RocketChatClientService;
) {
private RocketChatClientService: RocketChatClientService) {
this.headers = new HttpHeaders();
if (SessionStore.exist) {
this.ValidatedUser = SessionStore.user
console.log('login', SessionStore.user.RochetChatUser, SessionStore.user.Password)
this.RocketChatClientService.connect(()=>{
this.RocketChatClientService.login({
username: SessionStore.user.RochetChatUser,
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) {
@@ -78,6 +90,9 @@ export class AuthService {
session.Profile = 'MDGPR'
}
session.Password = user.password
session.RochetChatUser = user.username.split('@')[0]
session.BasicAuthKey = user.BasicAuthKey
SessionStore.reset(session)
@@ -99,6 +114,22 @@ export class AuthService {
"password": user.password,
}
this.RocketChatClientService.connect(()=>{
this.RocketChatClientService.login({
username: SessionStore.user.RochetChatUser,
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) {
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { ChatUserService } from './chat-user.service';
describe('ChatUserService', () => {
let service: ChatUserService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ChatUserService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
@@ -0,0 +1,9 @@
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ChatUserService {
constructor() { }
}
@@ -1,13 +1,13 @@
import { TestBed } from '@angular/core/testing';
import { MethodsService } from './methods.service';
import { ChatService } from './chat.service';
describe('MethodsService', () => {
let service: MethodsService;
describe('ChatService', () => {
let service: ChatService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(MethodsService);
service = TestBed.inject(ChatService);
});
it('should be created', () => {
+33
View File
@@ -0,0 +1,33 @@
import { Injectable } from '@angular/core';
import { RoomService } from './room.service';
import { RocketChatClientService } from 'src/app/services/socket/rocket-chat-client.service';
import { MessageService } from 'src/app/services/chat/message.service'
@Injectable({
providedIn: 'root'
})
export class ChatService {
rooms: RoomService[] = []
group = []
constructor(
private RocketChatClientService: RocketChatClientService
) {
this.getAllRoomAndSubscribe()
}
getAllRoomAndSubscribe() {
this.RocketChatClientService.getRooms().then((rooms: any) => {
rooms.result.update.forEach((roomData:any) => {
const room = new RoomService(new RocketChatClientService(), new MessageService())
room.setData({id: roomData.lastMessage.rid})
});
});
}
onJoinRoom() {
// live
}
}
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { MessageService } from './message.service';
describe('MessageService', () => {
let service: MessageService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(MessageService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
+9
View File
@@ -0,0 +1,9 @@
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class MessageService {
constructor() { }
}
-11
View File
@@ -1,11 +0,0 @@
import { Injectable } from '@angular/core';
import { ChatService } from '../chat.service';
@Injectable({
providedIn: 'root'
})
export class MethodsService {
constructor(private chatService: ChatService) {
}
}
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { RoomService } from './room.service';
describe('RoomService', () => {
let service: RoomService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(RoomService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
+28
View File
@@ -0,0 +1,28 @@
import { Injectable } from '@angular/core'
import { RocketChatClientService } from 'src/app/services/socket/rocket-chat-client.service';
import { MessageService } from 'src/app/services/chat/message.service'
import { ChatUserService } from 'src/app/services/chat/chat-user.service'
@Injectable({
providedIn: 'root'
})
export class RoomService {
massages: MessageService[] = []
chatUser: ChatUserService[] = []
id = []
constructor(
private RocketChatClientService: RocketChatClientService,
private MessageService: MessageService
) {}
setData({id}){
this.id= id
}
create() {}
sendMessage() {}
deleteMessage() {}
ReactToMessage() {}
}
@@ -1,18 +1,21 @@
import { Injectable } from '@angular/core';
import { v4 as uuidv4 } from 'uuid'
import { wsCallbacksParams, msgQueue } from 'src/app/models/rochet-chat-cliente-service'
class _RocketChatClientService {
import { deepFind } from 'src/plugin/deep'
/**
* Use this class to interact with rocketChat realtime API
*/
@Injectable({
providedIn: 'root'
})
export class RocketChatClientService {
private hasPing = false
private firstPingFunx: Function
isLogin = false;
request = []
constructor() {}
private returns() {}
connect(firstPingFunx: Function) {
this.hasPing = false
this.firstPingFunx = firstPingFunx
@@ -25,12 +28,12 @@ class _RocketChatClientService {
support: ["1"]
}
this.ws.send(connectMessage)
this.ws.send(connectMessage, 'connect', false)
this.ws.registerCallback({type:'Onmessage',funx:(message: any) => {
this.ws.registerCallback({type:'Onmessage', funx:(message: any) => {
if(message.msg == "ping") {
this.ws.send({msg:"pong"})
this.ws.send({msg:"pong"},'recoonect', false)
if(this.hasPing == false) {
// first ping
firstPingFunx()
@@ -45,6 +48,7 @@ class _RocketChatClientService {
login(user) {
const requestId = uuidv4()
this.isLogin = false
const loginRequest = {
msg: "method",
@@ -57,57 +61,35 @@ class _RocketChatClientService {
}
]
}
this.ws.send(loginRequest, requestId)
this.ws.send(loginRequest, requestId, false)
return new Promise((resolve, reject) => {
this.ws.registerCallback({type:'Onmessage', requestId, runOnces: true, funx:(message)=>{
// console.log(message.result.id, requestId)
if(message.id == requestId) { // same request send
if(message.result) {
if(message.result.token) {
this.isLogin = true
resolve(message)
} else {
this.isLogin = false
reject(message)
}
this.ws.registerCallback({type:'Onmessage', requestId, funx:(message)=>{
if(message.id == requestId || deepFind(message, 'result.id') == requestId) { // same request send
if(message.result.token) {
this.isLogin = true
setTimeout(()=>{
this.ws.wsMsgQueue()
},0)
resolve(message)
} else {
this.isLogin = false
reject(message)
}
reject(message)
return true
}
}})
});
}
logout(){}
send(roomId, message, option) {
const requestId = uuidv4()
var request = {
msg: "method",
method: "sendMessage",
id: requestId,
params: [{
_id: "message-id" || uuidv4(),
rid: "room-id" || roomId,
msg: "Hello World!" || message
}]
}
// this.ws.send(request, requestId);
return requestId;
}
subtribe() {}
joinRoom(){}
deleteMessage() {}
createRoom() {}
getRooms() {
const requestId = uuidv4()
const request = {
@@ -116,43 +98,186 @@ class _RocketChatClientService {
"id": requestId,
"params": [ { "$date": 1480377601 } ]
}
this.ws.send(request, requestId)
this.ws.send(request, requestId, true)
return new Promise((resolve, reject) => {
this.ws.registerCallback({type:'Onmessage', requestId, runOnces: true, funx:(message)=>{
if(message.result.id == requestId) { // same request send
this.ws.registerCallback({type:'Onmessage', requestId, funx:(message)=>{
if(message.id == requestId) { // same request send
resolve(message)
return true
}
}})
});
}
logout() {}
send(roomId, message) {
subscribe() {
const requestId = uuidv4()
var request = {
msg: "method",
method: "sendMessage",
id: requestId,
params: [{
_id: uuidv4(),
rid: roomId,
msg: message
}]
}
this.ws.send(request, requestId, true);
return new Promise((resolve, reject) => {
this.ws.registerCallback({type:'Onmessage', requestId, funx:(message)=>{
if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send
resolve(message)
return true
}
}})
});
}
joinRoom(){}
deleteMessage() {}
createRoom() {}
loadHistory(roomId, limit: number = 50) {
const requestId = uuidv4()
const request = {
msg: "method",
method: "loadHistory",
id: requestId,
params: [
roomId,
null,
limit,
{
"$date": 1480377601
}
]
}
this.ws.send(request, requestId, true)
return new Promise((resolve, reject) => {
this.ws.registerCallback({type:'Onmessage', requestId, funx:(message)=>{
// console.log(message)
if(message.id == requestId ) { // same request send
resolve(message)
return true
}
}})
});
}
subscribe(roomId : string) {
const requestId = uuidv4()
var subscribeRequest = {
"msg": "sub",
"id": requestId,
"name": "stream-notify-room",
"params":[
"room-id/event",
`${roomId}/event`,
false
]
}
this.ws.send(subscribeRequest);
this.ws.send(subscribeRequest, requestId, true);
return new Promise((resolve, reject) => {
this.ws.registerCallback({type:'Onmessage', requestId, runOnces: true, funx:(message)=>{
if(message.result.id == requestId) { // same request send
this.ws.registerCallback({type:'Onmessage', requestId, funx:(message)=>{
if(message.msg == 'ready' || deepFind(message, 'subs.0') == requestId) { // same request send
resolve(message)
return true
}
}})
});
}
private disconnect = () => {
/**
* @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 requestId = uuidv4()
const request = {
"msg": "sub",
"id": requestId,
"name": "stream-room-messages",
"params":[
`${roomId}/event`,
false
]
}
this.ws.send(request, requestId, true);
return new Promise((resolve, reject) => {
this.ws.registerCallback({type:'Onmessage', requestId, funx:(message)=>{
if(message.msg == 'ready' || deepFind(message, 'subs.0') == requestId) { // same request send
resolve(message)
return true
}
}})
});
}
streamNotifyRoom(roomId : string) {
const requestId = uuidv4()
let streamNotifyObj = {
"msg": "method",
"method": "stream-notify-room",
"id": requestId,
"params": [
`null/typing`,
"paulo.pinto",
true
]
};
this.ws.send(streamNotifyObj, requestId, true)
return new Promise((resolve, reject) => {
this.ws.registerCallback({type:'Onmessage', requestId, funx:(message)=>{
if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send
resolve('')
return true
}
}})
});
}
@@ -164,7 +289,7 @@ class _RocketChatClientService {
private ws = {
connected: false,
registerCallback:(params: wsCallbacksParams) =>{
registerCallback:(params: wsCallbacksParams) => {
if(!params.requestId) {
params.requestId = uuidv4()
@@ -172,12 +297,10 @@ class _RocketChatClientService {
this.wsCallbacks[params.requestId] = {
type: params.type,
funx: params.funx,
runOnces: params.runOnces
funx: params.funx
}
},
deleteCallback(){},
connect:(url)=> {
this.ws.connected = false
this.wsUrl = url
@@ -191,20 +314,28 @@ class _RocketChatClientService {
onopen:()=> {
this.ws.connected = true
console.log('================== welcome to socket server =====================')
this.wsMsgQueue.forEach((item, index, object) => {
this.ws.send(item.message, item.requestId);
object.splice(index, 1);
})
this.ws.wsMsgQueue()
},
send: (message: object, requestId = uuidv4()) => {
wsMsgQueue:()=> {
this.wsMsgQueue.forEach((item, index, object) => {
if(item.loginRequired == true && this.isLogin == true) {
console.log('run msgQueue ',index)
this.ws.send(item.message, item.requestId, item.loginRequired);
delete this.wsMsgQueue[item.requestId]
} else if(item.loginRequired == false) {
console.log('run msgQueue ',index)
this.ws.send(item.message, item.requestId, item.loginRequired);
delete this.wsMsgQueue[item.requestId]
}
if (this.ws.connected == false) { // save data to send when back online
})
},
send: (message: object, requestId = uuidv4(), loginRequired) => {
if (this.ws.connected == false || loginRequired == true && this.isLogin == false) { // save data to send when back online
console.log('save msgQueue')
this.wsMsgQueue.push({message, requestId})
this.wsMsgQueue.push({message, requestId, loginRequired})
} else {
console.log('send rocket chat', message)
let messageStr = JSON.stringify(message)
this.socket.send(messageStr)
}
@@ -213,16 +344,17 @@ class _RocketChatClientService {
onmessage:(event: any)=> {
const data = JSON.parse(event.data)
console.log('event.data', data)
// console.log('event.data', data)
for (const [key, value] of Object.entries(this.wsCallbacks)) {
if(value.type== 'Onmessage') {
value.funx(data)
if(value.runOnces) {
delete this.wsCallbacks[value.requestId || key]
}
}
const dontRepeat = value.funx(data)
if(dontRepeat) {
delete this.wsCallbacks[key]
}
}
}
@@ -243,6 +375,3 @@ class _RocketChatClientService {
}
}
export const RocketChatClientService = new _RocketChatClientService()