mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-20 21:35:50 +00:00
Merge branch 'feature/websocket' of https://bitbucket.org/equilibriumito/gabinete-digital into feature/websocket
This commit is contained in:
@@ -26,7 +26,7 @@ import { ScreenOrientation } from '@ionic-native/screen-orientation/ngx';
|
||||
import { SqliteService } from 'src/app/services/sqlite.service';
|
||||
import { Device } from '@capacitor/device';
|
||||
import { RouteService } from 'src/app/services/route.service';
|
||||
import { RocketChatClientService } from 'src/app/services/socket/rocket-chat-client.service';
|
||||
import { WsChatService } from 'src/app/services/chat/ws-chat.service';
|
||||
import { environment } from 'src/environments/environment';
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
|
||||
@@ -100,19 +100,8 @@ export class HomePage implements OnInit {
|
||||
private screenOrientation: ScreenOrientation,
|
||||
private sqliteservice: SqliteService,
|
||||
private RouteService: RouteService,
|
||||
private RocketChatClientService: RocketChatClientService) {
|
||||
window['jj'] = ()=> {
|
||||
//send message // roomId // Message
|
||||
//this.RocketChatClientService.send('fsMwcNdufWvdnChj7ya9nF9cX2HizxxWAM', 'Mensagem enviada programaticamente.'+ new Date().toISOString())
|
||||
}
|
||||
private WsChatService: WsChatService) {
|
||||
|
||||
|
||||
this.RocketChatClientService.registerCallback({type:'Onmessage',requestId:'asdfasdfasdf',funx:(message)=>{
|
||||
// console.log('message', message)
|
||||
}})
|
||||
|
||||
|
||||
// this.RocketChatClientService.send()
|
||||
/* this.webNotificationPopupService.askNotificationPermission() */
|
||||
|
||||
this.router.events.subscribe((val) => {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
<ion-content class="height-100 container-wrapper">
|
||||
<ion-refresher name="refresher" slot="fixed" (ionRefresh)="doRefresh($event)">
|
||||
<ion-progress-bar type="indeterminate" *ngIf="wsChatService.loadingWholeList"></ion-progress-bar>
|
||||
<ion-progress-bar type="indeterminate" *ngIf="wsChatMethodsService.loadingWholeList"></ion-progress-bar>
|
||||
<ion-refresher-content>
|
||||
</ion-refresher-content>
|
||||
</ion-refresher>
|
||||
@@ -48,7 +48,7 @@
|
||||
<ion-list *ngSwitchCase="'Contactos'">
|
||||
<ion-item-sliding>
|
||||
<div class="item item-hover width-100 d-flex ion-no-padding ion-no-margin"
|
||||
*ngFor="let room of wsChatService.individual | keyvalue"
|
||||
*ngFor="let room of wsChatMethodsService.dm | keyvalue"
|
||||
[class.item-active]="room.value.id == idSelected">
|
||||
<div class="item-icon">
|
||||
<ion-icon *ngIf="ThemeService.currentTheme == 'default' " class="icon" slot="start" src="assets/images/icons-chat-chat-40.svg"></ion-icon>
|
||||
@@ -89,7 +89,7 @@
|
||||
</div>
|
||||
</div>
|
||||
</ion-item-sliding>
|
||||
<ion-item-sliding *ngIf=" wsChatService.individualCount < 1">
|
||||
<ion-item-sliding *ngIf=" wsChatMethodsService.dmCount < 1">
|
||||
<div *ngFor="let n of numSequence(8); let i = index;" class="item item-hover width-100 d-flex ion-no-padding ion-no-margin">
|
||||
<div class="item-icon"><ion-icon class="icon" slot="start" src="assets/icon/icons-chat-grey.svg"></ion-icon></div>
|
||||
<div class="item-content flex-grow-1 cursor-pointer">
|
||||
|
||||
@@ -12,7 +12,7 @@ import {
|
||||
import { ModalController, Platform } from '@ionic/angular';
|
||||
import { AuthService } from 'src/app/services/auth.service';
|
||||
import { ChatService } from 'src/app/services/chat.service';
|
||||
import { wsChatService as wsChatService} from 'src/app/services/chat/chat.service'
|
||||
import { wsChatMethodsService as wsChatMethodsService} from 'src/app/services/chat/chat.service'
|
||||
import { GroupMessagesPage } from './group-messages/group-messages.page';
|
||||
import { ContactsPage } from './messages/contacts/contacts.page';
|
||||
import { MessagesPage } from './messages/messages.page';
|
||||
@@ -127,7 +127,7 @@ export class ChatPage implements OnInit {
|
||||
private sqlservice: SqliteService,
|
||||
private platform: Platform,
|
||||
private storageservice: StorageService,
|
||||
public wsChatService: wsChatService
|
||||
public wsChatMethodsService: wsChatMethodsService
|
||||
) {
|
||||
|
||||
this.loggedUserChat = authService.ValidatedUserChat['data'];
|
||||
|
||||
@@ -49,7 +49,7 @@
|
||||
</ion-refresher-content>
|
||||
</ion-refresher> -->
|
||||
<div (click)="handleClick()" class="messages" #scrollMe>
|
||||
<div class="messages-list-item-wrapper container-width-100" *ngFor="let msg of wsChatService.getRoom(this.roomId).massages; let last = last"
|
||||
<div class="messages-list-item-wrapper container-width-100" *ngFor="let msg of wsChatMethodsService.getRoom(this.roomId).massages; let last = last"
|
||||
[class.messages-list-item-wrapper-active]="msg._id == selectedMsgId" >
|
||||
<div (press)="handlePress(msg._id)" class='message-container incoming-{{msg.u.username!=loggedUser.me.username}}' (click)="openPreview(msg)" *ngIf="msg.msg !=''">
|
||||
<div class="title">
|
||||
|
||||
@@ -28,7 +28,7 @@ import { SqliteService } from 'src/app/services/sqlite.service';
|
||||
import { elementAt } from 'rxjs-compat/operator/elementAt';
|
||||
import { ViewMediaPage } from 'src/app/modals/view-media/view-media.page';
|
||||
import { ViewEventPage } from 'src/app/modals/view-event/view-event.page';
|
||||
import { wsChatService as wsChatService} from 'src/app/services/chat/chat.service'
|
||||
import { wsChatMethodsService } from 'src/app/services/chat/chat.service'
|
||||
|
||||
const IMAGE_DIR = 'stored-images';
|
||||
|
||||
@@ -96,7 +96,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
|
||||
private changeDetectorRef: ChangeDetectorRef,
|
||||
private platform: Platform,
|
||||
private sqlservice: SqliteService,
|
||||
public wsChatService: wsChatService
|
||||
public wsChatMethodsService: wsChatMethodsService
|
||||
) {
|
||||
this.loggedUser = authService.ValidatedUserChat['data'];
|
||||
this.roomId = this.navParams.get('roomId');
|
||||
@@ -108,7 +108,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
|
||||
}
|
||||
};
|
||||
|
||||
this.wsChatService.getRoom(this.roomId).loadHistory()
|
||||
this.wsChatMethodsService.getRoom(this.roomId).loadHistory()
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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 */
|
||||
@@ -3,7 +3,7 @@
|
||||
<div class="main-header">
|
||||
<div class="header-top">
|
||||
<div class="middle" *ngFor="let users of dmUsers">
|
||||
<ion-label class="title">{{ wsChatService.getRoom(roomId).name }}</ion-label>
|
||||
<ion-label class="title">{{ wsChatMethodsService.getRoom(roomId).name }}</ion-label>
|
||||
<span><ion-icon class="{{users.status}}" name="ellipse"></ion-icon></span>
|
||||
</div>
|
||||
<div hidden class="right">
|
||||
@@ -37,7 +37,7 @@
|
||||
</ion-refresher-content>
|
||||
</ion-refresher>
|
||||
<div class="messages" #scrollMe>
|
||||
<div class="messages-list-item-wrapper container-width-100" *ngFor="let msg of wsChatService.getRoom(roomId).massages; let last = last">
|
||||
<div class="messages-list-item-wrapper container-width-100" *ngFor="let msg of wsChatMethodsService.getRoom(roomId).massages; let last = last">
|
||||
<div class='message-item incoming-{{msg.u.username!=loggedUser.me.username}} max-width-45' *ngIf="msg.msg !=''">
|
||||
<div class="message-item-options d-flex justify-content-end">
|
||||
<fa-icon [matMenuTriggerFor]="beforeMenu" icon="chevron-down" class="message-options-icon cursor-pointer"></fa-icon>
|
||||
|
||||
@@ -19,7 +19,7 @@ import { ThemeService } from 'src/app/services/theme.service'
|
||||
import { PreviewCameraPage } from 'src/app/modals/preview-camera/preview-camera.page';
|
||||
import { ViewMediaPage } from 'src/app/modals/view-media/view-media.page';
|
||||
import { ViewEventPage } from 'src/app/modals/view-event/view-event.page';
|
||||
import { wsChatService as wsChatService} from 'src/app/services/chat/chat.service'
|
||||
import { wsChatMethodsService as wsChatMethodsService} from 'src/app/services/chat/chat.service'
|
||||
|
||||
@Component({
|
||||
selector: 'app-messages',
|
||||
@@ -78,13 +78,13 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
|
||||
public ThemeService: ThemeService,
|
||||
private changeDetectorRef: ChangeDetectorRef,
|
||||
private router: Router,
|
||||
public wsChatService: wsChatService
|
||||
public wsChatMethodsService: wsChatMethodsService
|
||||
) {
|
||||
|
||||
this.loggedUser = authService.ValidatedUserChat['data'];
|
||||
}
|
||||
ngOnChanges(changes: SimpleChanges): void {
|
||||
this.wsChatService.getRoom(this.roomId).loadHistory()
|
||||
this.wsChatMethodsService.getRoom(this.roomId).loadHistory()
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
@@ -214,7 +214,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
|
||||
}
|
||||
|
||||
sendMessage() {
|
||||
this.wsChatService.getRoom(this.roomId).send(this.message)
|
||||
this.wsChatMethodsService.getRoom(this.roomId).send(this.message)
|
||||
this.message = "";
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user