rocket chat service

This commit is contained in:
Peter Maquiran
2022-01-07 15:48:35 +01:00
parent 178e3cc559
commit ee8550f983
5 changed files with 165 additions and 187 deletions
@@ -1,31 +1,32 @@
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'
})
export class RocketChatClientService {
class _RocketChatClientService {
private SocketInterfaceService = new SocketInterfaceService()
connected = false
constructor() {
}
constructor() {}
private returns() {}
connect(url = 'wss://gabinetedigitalchat.dyndns.info/websocket') {
this.SocketInterfaceService.connect(url);
this.ws.connect(url);
const connectMessage = {
msg: "connect",
version: "1",
support: ["1"]
}
this.SocketInterfaceService.send(connectMessage)
this.ws.send(connectMessage)
this.ws.registerCallback('Onmessage',(message: any) => {
if(message.msg == "ping") {
this.ws.send({msg:"pong"})
}
})
}
login(user) {
@@ -45,7 +46,7 @@ export class RocketChatClientService {
}
]
}
this.SocketInterfaceService.send(loginRequest, requestId)
this.ws.send(loginRequest, requestId)
return requestId
}
@@ -66,7 +67,7 @@ export class RocketChatClientService {
}]
}
this.SocketInterfaceService.send(request, requestId);
// this.ws.send(request, requestId);
return requestId;
}
@@ -87,13 +88,94 @@ export class RocketChatClientService {
]
}
this.SocketInterfaceService.send(subscribeRequest);
//this.ws.send(subscribeRequest);
}
private disconnect = () => {
}
// socket ==================================================================
private socket!: WebSocket;
private url = ''
private callBacks: {
type: 'Offline' | 'Online' | 'Open' | 'Onmessage',
object?: string
funx: Function
}[] = []
msgQueue : {
message: object,
requestId: string
}[] = []
ws = {
registerCallback:(type: 'Offline' | 'Online' | 'Open' | 'Onmessage', funx: Function, object = '') =>{
this.callBacks.push({
type,
funx,
object
})
},
connect:(url)=> {
this.url = url
this.socket = new WebSocket(this.url);
// 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.connected = true
console.log('================== welcome to socket server =====================')
this.msgQueue.forEach((item, index, object) => {
this.ws.send(item.message, item.requestId);
object.splice(index, 1);
})
},
send: (message: object, requestId = uuidv4()) => {
if (this.connected == false) { // save data to send when back online
console.log('save msgQueue')
this.msgQueue.push({message, requestId})
} else {
console.log('send rocket chat', message)
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)
this.callBacks.forEach((e)=>{
if(e.type== 'Onmessage') {
e.funx(data)
}
})
},
onclose:(event: any)=> {
this.connected = false
console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
setTimeout(()=>{
// this.connect(this.url)
}, 500)
},
onerror: (event: any) => {
console.log(`[error] ${event.message}`);
}
}
}
window['RocketChatClientService'] = new RocketChatClientService();
export const RocketChatClientService = new _RocketChatClientService()