mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-20 05:16:07 +00:00
Merge branch 'feature/websocket' of https://bitbucket.org/equilibriumito/gabinete-digital into feature/websocket
This commit is contained in:
@@ -10,10 +10,7 @@ import { AlertController } from '@ionic/angular';
|
||||
import { SessionStore } from '../store/session.service';
|
||||
import { AESEncrypt } from '../services/aesencrypt.service';
|
||||
import { CookieService } from 'ngx-cookie-service';
|
||||
import { RocketChatClientService } from '../services/socket/rocket-chat-client.service';
|
||||
import { ChatService } from './chat.service';
|
||||
import { WebsocketService } from './websocket.service';
|
||||
|
||||
import { WsChatService } from 'src/app/services/chat/ws-chat.service';
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
@@ -34,8 +31,7 @@ export class AuthService {
|
||||
public alertController: AlertController,
|
||||
private aesencrypt: AESEncrypt,
|
||||
private cookieService: CookieService,
|
||||
private wsService: WebsocketService,
|
||||
private RocketChatClientService: RocketChatClientService) {
|
||||
private WsChatService: WsChatService) {
|
||||
|
||||
this.headers = new HttpHeaders();
|
||||
|
||||
@@ -43,9 +39,9 @@ export class AuthService {
|
||||
this.ValidatedUser = SessionStore.user
|
||||
|
||||
console.log('login', SessionStore.user.RochetChatUser, SessionStore.user.Password)
|
||||
this.RocketChatClientService.connect(()=>{
|
||||
this.WsChatService.connect(()=>{
|
||||
|
||||
this.RocketChatClientService.login({
|
||||
this.WsChatService.login({
|
||||
username: SessionStore.user.RochetChatUser,
|
||||
password: SessionStore.user.Password
|
||||
}).then((message) => {
|
||||
@@ -114,22 +110,6 @@ export class AuthService {
|
||||
this.ValidatedUser = null;
|
||||
}
|
||||
|
||||
loginWsChat(){
|
||||
let msg = {
|
||||
"msg": "method",
|
||||
"method": "login",
|
||||
"id":"42",
|
||||
"params":[
|
||||
{
|
||||
"user": { "username": "paulo.pinto" },
|
||||
"password": "tabteste@006"
|
||||
}
|
||||
]
|
||||
}
|
||||
this.wsService.messages.next(msg);
|
||||
this.isWsAuthenticated = true;
|
||||
}
|
||||
|
||||
//Login to rocketChat server
|
||||
async loginChat(user: UserForm): Promise<boolean> {
|
||||
let postData = {
|
||||
@@ -138,9 +118,9 @@ export class AuthService {
|
||||
}
|
||||
|
||||
|
||||
/* this.RocketChatClientService.connect(()=>{
|
||||
this.WsChatService.connect(()=>{
|
||||
|
||||
this.RocketChatClientService.login({
|
||||
this.WsChatService.login({
|
||||
username: SessionStore.user.RochetChatUser,
|
||||
password: user.password
|
||||
}).then((message) => {
|
||||
@@ -150,7 +130,7 @@ export class AuthService {
|
||||
}).finally(()=>{
|
||||
})
|
||||
|
||||
}) */
|
||||
})
|
||||
|
||||
|
||||
let responseChat = await this.httpService.post('login', postData).toPromise();
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { wsChatService } from './chat.service';
|
||||
import { wsChatMethodsService } from './chat.service';
|
||||
|
||||
describe('ChatService', () => {
|
||||
let service: wsChatService;
|
||||
let service: wsChatMethodsService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(wsChatService);
|
||||
service = TestBed.inject(wsChatMethodsService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { RoomService } from './room.service';
|
||||
import { RocketChatClientService } from 'src/app/services/socket/rocket-chat-client.service';
|
||||
import { WsChatService } from 'src/app/services/chat/ws-chat.service';
|
||||
import { MessageService } from 'src/app/services/chat/message.service'
|
||||
import { SessionStore } from 'src/app/store/session.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class wsChatService {
|
||||
export class wsChatMethodsService {
|
||||
|
||||
|
||||
individual: {[key: string]: RoomService} = {}
|
||||
dm: {[key: string]: RoomService} = {}
|
||||
group: {[key: string]: RoomService} = {}
|
||||
|
||||
loadingWholeList = false
|
||||
|
||||
individualCount = 0;
|
||||
dmCount = 0;
|
||||
groupCount = 0;
|
||||
|
||||
constructor(
|
||||
private RocketChatClientService: RocketChatClientService
|
||||
private WsChatService: WsChatService
|
||||
) {
|
||||
|
||||
(async()=>{
|
||||
@@ -32,17 +32,17 @@ export class wsChatService {
|
||||
async getAllRooms () {
|
||||
this.loadingWholeList = true
|
||||
|
||||
const rooms: any = await this.RocketChatClientService.getRooms();
|
||||
const rooms: any = await this.WsChatService.getRooms();
|
||||
|
||||
console.log(rooms)
|
||||
|
||||
rooms.result.update.forEach((roomData:any) => {
|
||||
let room:RoomService;
|
||||
|
||||
room = new RoomService(this.RocketChatClientService, new MessageService())
|
||||
room = new RoomService(this.WsChatService, new MessageService())
|
||||
room.setData({
|
||||
id: this.getRoomId(roomData),
|
||||
name: this.getChatName(roomData),
|
||||
name: this.getRoomName(roomData),
|
||||
lastMessage: this.getRoomLastMessage(roomData),
|
||||
_updatedAt: roomData._updatedAt['$date']
|
||||
})
|
||||
@@ -52,8 +52,8 @@ export class wsChatService {
|
||||
let roomId = roomData.lastMessage.rid
|
||||
|
||||
if(this.isIndividual(roomData)) {
|
||||
this.individual[roomId] = room
|
||||
this.individualCount++
|
||||
this.dm[roomId] = room
|
||||
this.dmCount++
|
||||
} else {
|
||||
this.group[roomId] = room
|
||||
this.groupCount++
|
||||
@@ -65,14 +65,14 @@ export class wsChatService {
|
||||
}
|
||||
|
||||
subscribeToRoom() {
|
||||
for (const id in this.individual) {
|
||||
this.RocketChatClientService.subscribe(id).then((subscription)=>{
|
||||
for (const id in this.dm) {
|
||||
this.WsChatService.subscribeNotifyRoom(id).then((subscription)=>{
|
||||
console.log('subscription', subscription)
|
||||
})
|
||||
}
|
||||
|
||||
for (const id in this.group) {
|
||||
this.RocketChatClientService.subscribe(id).then((subscription)=>{
|
||||
this.WsChatService.subscribeNotifyRoom(id).then((subscription)=>{
|
||||
console.log('subscription', subscription)
|
||||
})
|
||||
}
|
||||
@@ -80,13 +80,13 @@ export class wsChatService {
|
||||
|
||||
getRoom(id): RoomService {
|
||||
try {
|
||||
return this.individual[id]
|
||||
return this.dm[id]
|
||||
} catch(e) {
|
||||
return this.group[id]
|
||||
}
|
||||
}
|
||||
|
||||
getChatName(roomData) {
|
||||
getRoomName(roomData) {
|
||||
if(this.isIndividual(roomData)) {
|
||||
const names: String[] = roomData.usernames
|
||||
const roomName = names.filter((name)=>{
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Injectable } from '@angular/core'
|
||||
import { RocketChatClientService } from 'src/app/services/socket/rocket-chat-client.service';
|
||||
import { WsChatService } from 'src/app/services/chat/ws-chat.service';
|
||||
import { MessageService } from 'src/app/services/chat/message.service'
|
||||
import { ChatUserService } from 'src/app/services/chat/chat-user.service'
|
||||
import { showDateDuration } from 'src/plugin/showDateDuration'
|
||||
@@ -20,7 +20,7 @@ export class RoomService {
|
||||
|
||||
|
||||
constructor(
|
||||
public RocketChatClientService: RocketChatClientService,
|
||||
public WsChatService: WsChatService,
|
||||
private MessageService: MessageService,
|
||||
) {}
|
||||
|
||||
@@ -35,7 +35,7 @@ export class RoomService {
|
||||
}
|
||||
|
||||
receiveMessage() {
|
||||
this.RocketChatClientService.receiveLiveMessageFromRoom(
|
||||
this.WsChatService.receiveLiveMessageFromRoom(
|
||||
this.id,
|
||||
this.constructor.name+this.id,
|
||||
(Chatmessage) => {
|
||||
@@ -51,7 +51,7 @@ export class RoomService {
|
||||
}
|
||||
|
||||
send(msg) {
|
||||
this.RocketChatClientService.send(this.id, msg)
|
||||
this.WsChatService.send(this.id, msg)
|
||||
}
|
||||
|
||||
// runs onces only
|
||||
@@ -59,7 +59,7 @@ export class RoomService {
|
||||
|
||||
if(this.hasLoadHistory){ return false}
|
||||
|
||||
this.RocketChatClientService.loadHistory(this.id, limit).then((message:any) => {
|
||||
this.WsChatService.loadHistory(this.id, limit).then((message:any) => {
|
||||
console.log('loadHistory', message)
|
||||
|
||||
message.result.messages.reverse().forEach(element => {
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { WsChatMethodsService } from './ws-chat-methods.service';
|
||||
|
||||
describe('WsChatMethodsService', () => {
|
||||
let service: WsChatMethodsService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(WsChatMethodsService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,116 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { RoomService } from './room.service';
|
||||
import { WsChatService } from 'src/app/services/chat/ws-chat.service';
|
||||
import { MessageService } from 'src/app/services/chat/message.service'
|
||||
import { SessionStore } from 'src/app/store/session.service';
|
||||
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class WsChatMethodsService {
|
||||
|
||||
|
||||
|
||||
dm: {[key: string]: RoomService} = {}
|
||||
group: {[key: string]: RoomService} = {}
|
||||
|
||||
loadingWholeList = false
|
||||
|
||||
dmCount = 0;
|
||||
groupCount = 0;
|
||||
|
||||
constructor(
|
||||
private WsChatService: WsChatService
|
||||
) {
|
||||
|
||||
(async()=>{
|
||||
await this.getAllRooms();
|
||||
this.subscribeToRoom()
|
||||
})()
|
||||
|
||||
}
|
||||
|
||||
async getAllRooms () {
|
||||
this.loadingWholeList = true
|
||||
|
||||
const rooms: any = await this.WsChatService.getRooms();
|
||||
|
||||
console.log(rooms)
|
||||
|
||||
rooms.result.update.forEach((roomData:any) => {
|
||||
let room:RoomService;
|
||||
|
||||
room = new RoomService(this.WsChatService, new MessageService())
|
||||
room.setData({
|
||||
id: this.getRoomId(roomData),
|
||||
name: this.getRoomName(roomData),
|
||||
lastMessage: this.getRoomLastMessage(roomData),
|
||||
_updatedAt: roomData._updatedAt['$date']
|
||||
})
|
||||
|
||||
room.receiveMessage()
|
||||
|
||||
let roomId = roomData.lastMessage.rid
|
||||
|
||||
if(this.isIndividual(roomData)) {
|
||||
this.dm[roomId] = room
|
||||
this.dmCount++
|
||||
} else {
|
||||
this.group[roomId] = room
|
||||
this.groupCount++
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
this.loadingWholeList = false
|
||||
}
|
||||
|
||||
subscribeToRoom() {
|
||||
for (const id in this.dm) {
|
||||
this.WsChatService.subscribeNotifyRoom(id).then((subscription)=>{
|
||||
console.log('subscription', subscription)
|
||||
})
|
||||
}
|
||||
|
||||
for (const id in this.group) {
|
||||
this.WsChatService.subscribeNotifyRoom(id).then((subscription)=>{
|
||||
console.log('subscription', subscription)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
getRoom(id): RoomService {
|
||||
try {
|
||||
return this.dm[id]
|
||||
} catch(e) {
|
||||
return this.group[id]
|
||||
}
|
||||
}
|
||||
|
||||
getRoomName(roomData) {
|
||||
if(this.isIndividual(roomData)) {
|
||||
const names: String[] = roomData.usernames
|
||||
const roomName = names.filter((name)=>{
|
||||
return name != SessionStore.user.RochetChatUser
|
||||
})[0]
|
||||
|
||||
return roomName
|
||||
} else {
|
||||
return roomData.fName
|
||||
}
|
||||
}
|
||||
|
||||
getRoomId(roomData) {
|
||||
return roomData.lastMessage.rid
|
||||
}
|
||||
|
||||
getRoomLastMessage(roomData) {
|
||||
return roomData.lastMessage
|
||||
}
|
||||
|
||||
private isIndividual(roomData) {
|
||||
return !roomData.fname
|
||||
}
|
||||
|
||||
}
|
||||
+4
-4
@@ -1,13 +1,13 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { SynchroService } from './synchro.service';
|
||||
import { WsChatService } from './ws-chat.service';
|
||||
|
||||
describe('SynchroService', () => {
|
||||
let service: SynchroService;
|
||||
describe('WsChatService', () => {
|
||||
let service: WsChatService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(SynchroService);
|
||||
service = TestBed.inject(WsChatService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
+5
-9
@@ -3,13 +3,11 @@ import { v4 as uuidv4 } from 'uuid'
|
||||
import { wsCallbacksParams, msgQueue } from 'src/app/models/rochet-chat-cliente-service'
|
||||
import { deepFind } from 'src/plugin/deep'
|
||||
import { environment } from 'src/environments/environment';
|
||||
/**
|
||||
* Use this class to interact with rocketChat realtime API
|
||||
*/
|
||||
@Injectable({
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class RocketChatClientService {
|
||||
export class WsChatService {
|
||||
|
||||
|
||||
private firstPingFunx: Function
|
||||
isLogin = false;
|
||||
@@ -164,7 +162,7 @@ import { environment } from 'src/environments/environment';
|
||||
});
|
||||
}
|
||||
|
||||
subscribe(roomId : string) {
|
||||
subscribeNotifyRoom(roomId : string) {
|
||||
|
||||
const requestId = uuidv4()
|
||||
|
||||
@@ -362,6 +360,4 @@ import { environment } from 'src/environments/environment';
|
||||
console.log(`[error] ${event.message}`);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}}
|
||||
@@ -1,16 +0,0 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { RocketChatClientService } from './rocket-chat-client.service';
|
||||
|
||||
describe('RocketChatClientService', () => {
|
||||
let service: RocketChatClientService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(RocketChatClientService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -1,215 +0,0 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { SessionStore } from 'src/app/store/session.service';
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import { BackgroundService } from '../background.service';
|
||||
import { environment } from 'src/environments/environment';
|
||||
import { EventTrigger } from '../eventTrigger.service'
|
||||
|
||||
export interface wss {
|
||||
|
||||
url: string,
|
||||
type: 'reflect' | 'emit'
|
||||
header: {
|
||||
id: string
|
||||
bluePrint: string,
|
||||
jwt: string
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
class SynchroService {
|
||||
/* [x: string]: any;
|
||||
|
||||
private connection!: WebSocket;
|
||||
private id: string = uuidv4();
|
||||
public conected = false
|
||||
private url: string = ''
|
||||
callback = function () { }
|
||||
private _connected = false;
|
||||
private eventtrigger: EventTrigger;
|
||||
private BackgroundService = new BackgroundService()
|
||||
callBacks: {
|
||||
type: 'Offline' | 'Online' | 'Onmessage' | 'Chat' | 'Notification' | 'Notifications' | '',
|
||||
object?: string
|
||||
funx: Function
|
||||
}[] = []
|
||||
private msgQueue = []
|
||||
|
||||
constructor() {
|
||||
// alert(SessionStore.user.FullName)
|
||||
}
|
||||
|
||||
get connected() {
|
||||
return this._connected
|
||||
}
|
||||
|
||||
setUrl() {
|
||||
|
||||
let header = {
|
||||
id: '1234',
|
||||
bluePrint: '12312123',
|
||||
jwt: uuidv4()
|
||||
}
|
||||
|
||||
let wss: wss = {
|
||||
header,
|
||||
url: 'wss://sychro-offline.herokuapp.com/ws/some_url/',
|
||||
type: 'reflect'
|
||||
}
|
||||
|
||||
this.url = `${wss.url}${wss.header.id}/${wss.header.jwt}/${wss.header.bluePrint}/${this.id}/`
|
||||
}
|
||||
|
||||
registerCallback(type: 'Offline' | 'Online' | 'Onmessage' | 'Chat' | 'Notifications' | 'Notification', funx: Function, object = '') {
|
||||
this.callBacks.push({
|
||||
type,
|
||||
funx,
|
||||
object
|
||||
})
|
||||
}
|
||||
|
||||
connect() {
|
||||
|
||||
this.connection = new WebSocket(this.url);
|
||||
// bind function
|
||||
this.connection.onopen = this.onopen;
|
||||
this.connection.onmessage = this.onmessage;
|
||||
this.connection.onclose = this.onclose;
|
||||
this.connection.onerror = this.onerror;
|
||||
}
|
||||
|
||||
private onopen = () => {
|
||||
|
||||
|
||||
|
||||
//if (this._connected === true) {
|
||||
//this.BackgroundService.online()
|
||||
console.log('Online', this._connected)
|
||||
this.callBacks.forEach((e) => {
|
||||
if (e.type == 'Online') {
|
||||
e.funx()
|
||||
}
|
||||
})
|
||||
//}
|
||||
|
||||
console.log('open ======================= welcome to socket server')
|
||||
|
||||
this._connected = true
|
||||
|
||||
// send all saved data due to internet connection
|
||||
this.msgQueue.forEach((item, index, object) => {
|
||||
this.$send(item);
|
||||
object.splice(index, 1);
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
public $send(object: any) {
|
||||
|
||||
if (!this._connected) { // save data to send when back online
|
||||
this.msgQueue.push(object)
|
||||
}
|
||||
|
||||
let payload = {
|
||||
message: JSON.stringify(object) || '{"person.adress.country":"1Angola"}',
|
||||
username: SessionStore.user.FullName,
|
||||
idConnection: this.id
|
||||
}
|
||||
|
||||
|
||||
let sendData = JSON.stringify(payload);
|
||||
console.log(sendData)
|
||||
|
||||
this.connection.send(sendData);
|
||||
}
|
||||
|
||||
private onmessage = async (event: any) => {
|
||||
|
||||
|
||||
let data = JSON.parse(event.data)
|
||||
let payload = JSON.parse(data.message)
|
||||
payload.message = JSON.parse(payload.message)
|
||||
const idConnection = payload.idConnection
|
||||
const username = payload.username
|
||||
|
||||
if (idConnection != this.id) {
|
||||
|
||||
if (window['platform'].is('desktop') || this.platform.is('mobileweb')) { }
|
||||
else return false
|
||||
|
||||
if (environment.production) return false
|
||||
|
||||
this.callBacks.forEach((e) => {
|
||||
|
||||
if (payload.message[0]) {
|
||||
if (payload.message[0].Service && payload.message[0].Object && payload.message[0].IdObject) {
|
||||
if (e.type == '' && !e.object) {
|
||||
if (username == SessionStore.user.FullName) {
|
||||
e.funx(payload.message, data)
|
||||
}
|
||||
}
|
||||
|
||||
if (e.type == 'Notifications') {
|
||||
e.funx(payload.message, data)
|
||||
}
|
||||
|
||||
}
|
||||
} else if (payload.message.Service && payload.message.Object && payload.message.IdObject) {
|
||||
if (e.type == 'Notification' && e.object == payload.message.Object || e.type == 'Notification' && e.object == 'any') {
|
||||
e.funx(payload.message, data)
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
this.callback()
|
||||
}
|
||||
|
||||
private onclose = (event: any) => {
|
||||
console.log('Websocket close')
|
||||
setTimeout(() => {
|
||||
if (event.wasClean) {
|
||||
console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
|
||||
} else {
|
||||
// e.g. server process killed or network down
|
||||
// event.code is usually 1006 in this case
|
||||
console.log('[close] Connection died');
|
||||
console.log('Reconnect')
|
||||
|
||||
|
||||
|
||||
// if (this._connected === false) {
|
||||
// this.BackgroundService.offline();
|
||||
console.log('Offline', this._connected)
|
||||
this.callBacks.forEach((e) => {
|
||||
if (e.type == 'Offline') {
|
||||
e.funx()
|
||||
}
|
||||
})
|
||||
//}
|
||||
|
||||
|
||||
// status
|
||||
this._connected = false
|
||||
// reconnect
|
||||
this.connect()
|
||||
|
||||
}
|
||||
}, 100);
|
||||
}
|
||||
|
||||
private onerror = (event: any) => {
|
||||
console.log(`[error] ${event.message}`);
|
||||
} */
|
||||
}
|
||||
|
||||
|
||||
/* export const synchro = new SynchroService()
|
||||
synchro.setUrl()
|
||||
synchro.connect()
|
||||
|
||||
window['synchro'] = synchro */
|
||||
Reference in New Issue
Block a user