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-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-07 15:48:35 +01:00
|
|
|
connected = false
|
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
|
|
|
|
|
|
|
|
connect(url = 'wss://gabinetedigitalchat.dyndns.info/websocket') {
|
|
|
|
|
|
2022-01-07 15:48:35 +01:00
|
|
|
this.ws.connect(url);
|
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)
|
|
|
|
|
|
|
|
|
|
this.ws.registerCallback('Onmessage',(message: any) => {
|
|
|
|
|
if(message.msg == "ping") {
|
|
|
|
|
this.ws.send({msg:"pong"})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
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: [
|
|
|
|
|
{
|
|
|
|
|
"user": { "username": user.username },
|
|
|
|
|
"password": {
|
|
|
|
|
"digest": user.password,
|
|
|
|
|
"algorithm":"sha-256"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logout(){ }
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
receive() {}
|
|
|
|
|
joinRoom(){}
|
|
|
|
|
deleteMessage() {}
|
2022-01-07 11:21:14 +01:00
|
|
|
createRoom() {}
|
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 15:48:35 +01:00
|
|
|
// 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}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-07 09:03:15 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-07 15:48:35 +01:00
|
|
|
|
|
|
|
|
export const RocketChatClientService = new _RocketChatClientService()
|