2022-01-07 09:03:15 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
2022-01-07 11:21:14 +01:00
|
|
|
import { v4 as uuidv4 } from 'uuid'
|
2022-01-10 09:35:17 +01:00
|
|
|
import { wsCallbacksParams, msgQueue } from 'src/app/models/rochet-chat-cliente-service'
|
2022-01-07 09:03:15 +01:00
|
|
|
|
2022-01-07 15:48:35 +01:00
|
|
|
class _RocketChatClientService {
|
2022-01-07 09:03:15 +01:00
|
|
|
|
2022-01-10 08:34:30 +01:00
|
|
|
private hasPing = false
|
|
|
|
|
private firstPingFunx: Function
|
2022-01-10 09:35:17 +01:00
|
|
|
isLogin = false;
|
|
|
|
|
request = []
|
2022-01-07 09:03:15 +01:00
|
|
|
|
2022-01-07 15:48:35 +01:00
|
|
|
constructor() {}
|
2022-01-07 14:29:41 +01:00
|
|
|
|
|
|
|
|
private returns() {}
|
2022-01-07 11:21:14 +01:00
|
|
|
|
2022-01-10 08:34:30 +01:00
|
|
|
connect(firstPingFunx: Function) {
|
|
|
|
|
this.hasPing = false
|
|
|
|
|
this.firstPingFunx = firstPingFunx
|
2022-01-07 11:21:14 +01:00
|
|
|
|
2022-01-07 16:34:02 +01:00
|
|
|
this.ws.connect('wss://gabinetedigitalchat.dyndns.info/websocket');
|
2022-01-07 11:21:14 +01:00
|
|
|
|
|
|
|
|
const connectMessage = {
|
|
|
|
|
msg: "connect",
|
|
|
|
|
version: "1",
|
|
|
|
|
support: ["1"]
|
|
|
|
|
}
|
2022-01-07 15:48:35 +01:00
|
|
|
|
|
|
|
|
this.ws.send(connectMessage)
|
2022-01-07 16:34:02 +01:00
|
|
|
|
2022-01-10 09:35:17 +01:00
|
|
|
this.ws.registerCallback({type:'Onmessage',funx:(message: any)=>{
|
|
|
|
|
|
2022-01-07 15:48:35 +01:00
|
|
|
if(message.msg == "ping") {
|
|
|
|
|
this.ws.send({msg:"pong"})
|
2022-01-07 16:34:02 +01:00
|
|
|
if(this.hasPing == false) {
|
|
|
|
|
// first ping
|
2022-01-10 08:34:30 +01:00
|
|
|
firstPingFunx()
|
2022-01-10 10:22:17 +01:00
|
|
|
|
|
|
|
|
setTimeout(()=>{
|
|
|
|
|
this.getRooms()
|
|
|
|
|
}, 5000)
|
2022-01-07 16:34:02 +01:00
|
|
|
this.hasPing = true
|
|
|
|
|
}
|
2022-01-07 15:48:35 +01:00
|
|
|
}
|
2022-01-10 09:35:17 +01:00
|
|
|
|
|
|
|
|
}})
|
2022-01-07 15:48:35 +01:00
|
|
|
|
2022-01-07 09:03:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
login(user) {
|
2022-01-07 14:29:41 +01:00
|
|
|
const requestId = uuidv4()
|
|
|
|
|
|
2022-01-07 09:03:15 +01:00
|
|
|
const loginRequest = {
|
|
|
|
|
msg: "method",
|
|
|
|
|
method: "login",
|
2022-01-07 14:29:41 +01:00
|
|
|
id: requestId,
|
2022-01-07 09:03:15 +01:00
|
|
|
params: [
|
|
|
|
|
{
|
2022-01-10 10:22:17 +01:00
|
|
|
user: { username: user.username },
|
|
|
|
|
password: user.password
|
2022-01-07 09:03:15 +01:00
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
2022-01-07 15:48:35 +01:00
|
|
|
this.ws.send(loginRequest, requestId)
|
2022-01-07 14:29:41 +01:00
|
|
|
|
|
|
|
|
return requestId
|
2022-01-07 09:03:15 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-07 16:34:02 +01:00
|
|
|
logout(){}
|
2022-01-07 09:03:15 +01:00
|
|
|
|
2022-01-07 11:21:14 +01:00
|
|
|
send(roomId, message, option) {
|
2022-01-07 14:29:41 +01:00
|
|
|
const requestId = uuidv4()
|
|
|
|
|
|
2022-01-07 11:21:14 +01:00
|
|
|
var request = {
|
2022-01-07 14:29:41 +01:00
|
|
|
msg: "method",
|
|
|
|
|
method: "sendMessage",
|
|
|
|
|
id: requestId,
|
|
|
|
|
params: [{
|
|
|
|
|
_id: "message-id" || uuidv4(),
|
|
|
|
|
rid: "room-id" || roomId,
|
|
|
|
|
msg: "Hello World!" || message
|
2022-01-07 11:21:14 +01:00
|
|
|
}]
|
2022-01-07 14:29:41 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-07 15:48:35 +01:00
|
|
|
// this.ws.send(request, requestId);
|
2022-01-07 11:21:14 +01:00
|
|
|
|
2022-01-07 14:29:41 +01:00
|
|
|
return requestId;
|
2022-01-07 11:21:14 +01:00
|
|
|
}
|
2022-01-07 09:03:15 +01:00
|
|
|
|
2022-01-10 10:22:17 +01:00
|
|
|
subtribe() {}
|
2022-01-07 09:03:15 +01:00
|
|
|
joinRoom(){}
|
|
|
|
|
deleteMessage() {}
|
2022-01-07 11:21:14 +01:00
|
|
|
createRoom() {}
|
2022-01-07 14:29:41 +01:00
|
|
|
|
2022-01-10 08:34:30 +01:00
|
|
|
getRooms() {
|
2022-01-10 10:22:17 +01:00
|
|
|
const requestId = uuidv4()
|
2022-01-10 08:34:30 +01:00
|
|
|
|
2022-01-10 10:22:17 +01:00
|
|
|
const request = {
|
|
|
|
|
"msg": "method",
|
|
|
|
|
"method": "rooms/get",
|
|
|
|
|
"id": requestId,
|
|
|
|
|
"params": [ { "$date": 1480377601 } ]
|
|
|
|
|
}
|
|
|
|
|
this.ws.send(request, requestId)
|
2022-01-10 08:34:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-07 14:29:41 +01:00
|
|
|
subscribe() {
|
|
|
|
|
var subscribeRequest = {
|
|
|
|
|
"msg": "sub",
|
|
|
|
|
"id": "unique-id",
|
|
|
|
|
"name": "stream-notify-room",
|
|
|
|
|
"params":[
|
|
|
|
|
"room-id/event",
|
|
|
|
|
false
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-07 15:48:35 +01:00
|
|
|
//this.ws.send(subscribeRequest);
|
2022-01-07 14:29:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private disconnect = () => {
|
|
|
|
|
|
|
|
|
|
}
|
2022-01-07 09:03:15 +01:00
|
|
|
|
2022-01-07 16:34:02 +01:00
|
|
|
// socket class ==================================================================
|
2022-01-07 15:48:35 +01:00
|
|
|
private socket!: WebSocket;
|
2022-01-10 09:35:17 +01:00
|
|
|
private wsUrl = ''
|
2022-01-10 10:22:17 +01:00
|
|
|
private wsMsgQueue : msgQueue[] = []
|
2022-01-10 09:35:17 +01:00
|
|
|
private wsCallbacks: {[key: string]: wsCallbacksParams} = {}
|
2022-01-07 15:48:35 +01:00
|
|
|
|
2022-01-10 10:22:17 +01:00
|
|
|
private ws = {
|
2022-01-10 09:35:17 +01:00
|
|
|
connected: false,
|
|
|
|
|
registerCallback:(params: wsCallbacksParams) =>{
|
|
|
|
|
|
|
|
|
|
if(!params.requestId) {
|
|
|
|
|
params.requestId = uuidv4()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.wsCallbacks[params.requestId] = {
|
|
|
|
|
type: params.type,
|
|
|
|
|
funx: params.funx,
|
|
|
|
|
options: params.options || {}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-07 15:48:35 +01:00
|
|
|
},
|
2022-01-10 09:35:17 +01:00
|
|
|
deleteCallback(){},
|
2022-01-07 15:48:35 +01:00
|
|
|
connect:(url)=> {
|
2022-01-10 09:35:17 +01:00
|
|
|
this.ws.connected = false
|
|
|
|
|
this.wsUrl = url
|
|
|
|
|
this.socket = new WebSocket(this.wsUrl);
|
2022-01-07 15:48:35 +01:00
|
|
|
// 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:()=> {
|
2022-01-10 09:35:17 +01:00
|
|
|
this.ws.connected = true
|
2022-01-07 15:48:35 +01:00
|
|
|
console.log('================== welcome to socket server =====================')
|
|
|
|
|
|
2022-01-10 09:35:17 +01:00
|
|
|
this.wsMsgQueue.forEach((item, index, object) => {
|
2022-01-07 15:48:35 +01:00
|
|
|
this.ws.send(item.message, item.requestId);
|
|
|
|
|
object.splice(index, 1);
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
send: (message: object, requestId = uuidv4()) => {
|
|
|
|
|
|
2022-01-10 09:35:17 +01:00
|
|
|
if (this.ws.connected == false) { // save data to send when back online
|
2022-01-07 15:48:35 +01:00
|
|
|
console.log('save msgQueue')
|
2022-01-10 09:35:17 +01:00
|
|
|
this.wsMsgQueue.push({message, requestId})
|
2022-01-07 15:48:35 +01:00
|
|
|
} else {
|
2022-01-10 09:35:17 +01:00
|
|
|
// console.log('send rocket chat', message)
|
2022-01-07 15:48:35 +01:00
|
|
|
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)
|
|
|
|
|
|
2022-01-10 09:35:17 +01:00
|
|
|
for (const [key, value] of Object.entries(this.wsCallbacks)) {
|
|
|
|
|
if(value.type== 'Onmessage') {
|
|
|
|
|
value.funx(data)
|
2022-01-07 15:48:35 +01:00
|
|
|
}
|
2022-01-10 09:35:17 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-07 15:48:35 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onclose:(event: any)=> {
|
2022-01-10 08:34:30 +01:00
|
|
|
|
|
|
|
|
this.connect(this.firstPingFunx())
|
|
|
|
|
|
2022-01-10 09:35:17 +01:00
|
|
|
this.ws.connected = false
|
2022-01-07 15:48:35 +01:00
|
|
|
console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onerror: (event: any) => {
|
|
|
|
|
console.log(`[error] ${event.message}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-07 09:03:15 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-07 15:48:35 +01:00
|
|
|
|
|
|
|
|
export const RocketChatClientService = new _RocketChatClientService()
|