improve rocket-chat-cliente interface

This commit is contained in:
Peter Maquiran
2022-01-07 11:21:14 +01:00
parent 6e0b54c0cf
commit d951bf6cc1
7 changed files with 158 additions and 56 deletions
@@ -9,7 +9,7 @@ export class SocketInterfaceService {
private url = ''
private connected = false
private callBacks: {
type: 'Offline' | 'Online' | 'Onmessage' | 'Chat' | 'Notification' | 'Notifications' | '',
type: 'Offline' | 'Online' | 'Open' ,
object?: string
funx: Function
}[] = []
@@ -17,6 +17,14 @@ export class SocketInterfaceService {
constructor() { }
registerCallback(type: 'Offline' | 'Online' | 'Open', funx: Function, object = '') {
this.callBacks.push({
type,
funx,
object
})
}
connect(url) {
this.url = url
@@ -28,8 +36,20 @@ export class SocketInterfaceService {
this.socket.onerror = this.onerror;
}
onopen() {
this.connected = true
// send all saved data due to internet connection
this.msgQueue.forEach((item, index, object) => {
this.send(item);
object.splice(index, 1);
})
}
send(message: object) {
if (!this.connected) { // save data to send when back online
console.log(this.connected, message)
if (this.connected === false) { // save data to send when back online
this.msgQueue.push(message)
} else {
let messageStr = JSON.stringify(message)
@@ -37,12 +57,12 @@ export class SocketInterfaceService {
}
}
onopen() {
onmessage(event: any) {
console.log('event.data', JSON.parse(event.data))
}
onmessage() {}
onclose(event: any) {
this.connected = false
console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
setTimeout(()=>{
this.connect(this.url)
@@ -1,6 +1,7 @@
import { Injectable } from '@angular/core';
import { SocketInterfaceService } from './interface/socket-interface.service'
import { environment } from 'src/environments/environment';
import { v4 as uuidv4 } from 'uuid'
@Injectable({
providedIn: 'root'
@@ -9,16 +10,25 @@ export class RocketChatClientService {
private SocketInterfaceService = new SocketInterfaceService()
constructor() {
const url = environment.apiChatUrl.replace('https','wss')
// this.SocketInterfaceService.connect(url);
constructor() {}
connect(url = 'wss://gabinetedigitalchat.dyndns.info/websocket') {
this.SocketInterfaceService.connect(url);
const connectMessage = {
msg: "connect",
version: "1",
support: ["1"]
}
this.SocketInterfaceService.send(connectMessage)
}
login(user) {
const loginRequest = {
msg: "method",
method: "login",
id: "42",
id: uuidv4(),
params: [
{
"user": { "username": user.username },
@@ -29,17 +39,31 @@ export class RocketChatClientService {
}
]
}
// this.SocketInterfaceService.send(loginRequest)
this.SocketInterfaceService.send(loginRequest)
}
logout(){ }
send(roomId, message, option) {}
send(roomId, message, option) {
var request = {
"msg": "method",
"method": "sendMessage",
"id": uuidv4(),
"params": [{
"_id": "message-id",
"rid": "room-id",
"msg": "Hello World!"
}]
}
this.SocketInterfaceService.send(request);
}
receive() {}
joinRoom(){}
deleteMessage() {}
createRoom() {}
}