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

This commit is contained in:
tiago.kayaya
2022-01-12 12:51:08 +01:00
19 changed files with 188 additions and 322 deletions
+3 -3
View File
@@ -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', () => {
+15 -15
View File
@@ -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)=>{
+5 -5
View File
@@ -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 { WebsocketService } from './websocket.service';
describe('WebsocketService', () => {
let service: WebsocketService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(WebsocketService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
@@ -0,0 +1,43 @@
import { Injectable } from '@angular/core';
import * as Rx from 'rxjs/Rx';
@Injectable({
providedIn: 'root'
})
export class WebsocketService {
constructor() { }
private subject: Rx.Subject<MessageEvent>;
public connect(url): Rx.Subject<MessageEvent> {
if(!this.subject){
this.subject = this.create(url);
console.log("Sucessful connect :"+url);
}
return this.subject;
}
private create(url): Rx.Subject<MessageEvent>{
let ws = new WebSocket(url);
let observable = Rx.Observable.create(
(obs: Rx.Observer<MessageEvent>) => {
ws.onmessage = obs.next.bind(obs);
ws.onerror = obs.error.bind(obs);
ws.onclose = obs.complete.bind(obs);
return ws.close.bind(ws)
})
let observer = {
next: (data: Object) => {
if(ws.readyState === WebSocket.OPEN){
ws.send(JSON.stringify(data));
}
}
}
return Rx.Subject.create(observer, observable);
}
}
@@ -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
}
}
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { WsChatService } from './ws-chat.service';
describe('WsChatService', () => {
let service: WsChatService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(WsChatService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
+363
View File
@@ -0,0 +1,363 @@
import { Injectable } from '@angular/core';
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';
@Injectable({
providedIn: 'root'
})
export class WsChatService {
private firstPingFunx: Function
isLogin = false;
constructor() {}
connect(firstPingFunx: Function) {
this.firstPingFunx = firstPingFunx //
this.ws.connect(environment.apiWsChatUrl);
const connectMessage = {
msg: "connect",
version: "1",
support: ["1"]
}
this.ws.send(connectMessage, 'connect', false)
this.ws.send({msg:"pong"},'recoonect', false)
firstPingFunx()
}
login(user) {
const requestId = uuidv4()
this.isLogin = false
const loginRequest = {
msg: "method",
method: "login",
id: requestId,
params: [
{
user: { username: user.username },
password: user.password
}
]
}
this.ws.send(loginRequest, requestId, false)
return new Promise((resolve, reject) => {
this.ws.registerCallback({type:'Onmessage', requestId, funx:(message)=>{
if(message.id == requestId ) { // same request send
if(message.result.token) {
this.isLogin = true
this.ws.wsMsgQueue()
resolve(message)
} else {
this.isLogin = false
reject(message)
}
reject(message)
return true
}
}})
});
}
getRooms(roomOlder = 1480377601) {
const requestId = uuidv4()
const request = {
"msg": "method",
"method": "rooms/get",
"id": requestId,
"params": [ { "$date": 1480377601 } ]
}
this.ws.send(request, requestId, true)
return new Promise((resolve, reject) => {
this.ws.registerCallback({type:'Onmessage', requestId, funx:(message)=>{
if(message.id == requestId) { // same request send
resolve(message)
return true
}
}})
});
}
logout() {}
send(roomId, message) {
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
}
}})
});
}
subscribeNotifyRoom(roomId : string) {
const requestId = uuidv4()
var subscribeRequest = {
"msg": "sub",
"id": requestId,
"name": "stream-notify-room",
"params":[
`${roomId}/event`,
false
]
}
this.ws.send(subscribeRequest, 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
}
}})
});
}
/**
* @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
}
}})
});
}
registerCallback(data:wsCallbacksParams) {
return this.ws.registerCallback(data)
}
// socket class ==================================================================
private socket!: WebSocket;
private wsUrl = ''
private wsMsgQueue : msgQueue[] = []
private wsCallbacks: {[key: string]: wsCallbacksParams} = {}
private ws = {
connected: false,
registerCallback:(params: wsCallbacksParams) => {
if(!params.requestId) {
params.requestId = uuidv4()
}
this.wsCallbacks[params.requestId] = {
type: params.type,
funx: params.funx
}
},
connect:(url)=> {
this.wsUrl = url
this.socket = new WebSocket(this.wsUrl);
// bind function
this.socket.onopen = this.ws.onopen;
this.socket.onmessage = this.ws.onmessage;
this.socket.onclose = this.ws.onclose;
this.socket.onerror = this.ws.onerror;
},
onopen:()=> {
this.ws.connected = true
console.log('================== welcome to socket server =====================')
this.ws.wsMsgQueue()
},
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]
}
})
},
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.ws.connected == false || loginRequired == true && this.isLogin == false',this.ws.connected, loginRequired, this.isLogin)
this.wsMsgQueue.push({message, requestId, loginRequired})
} else {
let messageStr = JSON.stringify(message)
this.socket.send(messageStr)
}
return requestId
},
onmessage:(event: any)=> {
const data = JSON.parse(event.data)
// console.log('event.data', data)
for (const [key, value] of Object.entries(this.wsCallbacks)) {
if(value.type== 'Onmessage') {
const dontRepeat = value.funx(data)
if(dontRepeat) {
delete this.wsCallbacks[key]
}
}
}
},
onclose:(event: any)=> {
this.connect(this.firstPingFunx())
this.ws.connected = false
console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
},
onerror: (event: any) => {
console.log(`[error] ${event.message}`);
}
}}