mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-20 05:16:07 +00:00
Merge branch 'feature/websocket' of https://bitbucket.org/equilibriumito/gabinete-digital into feature/websocket
This commit is contained in:
@@ -98,10 +98,22 @@ export class HomePage implements OnInit {
|
||||
private RouteService: RouteService) {
|
||||
|
||||
this.RocketChatClientService.connect(()=>{
|
||||
|
||||
this.RocketChatClientService.login({
|
||||
username: 'paulo.pinto@gabinetedigital.local',
|
||||
username: 'paulo.pinto',
|
||||
email: 'paulo.pinto@gabinetedigital.local',
|
||||
password: 'tabteste@006'
|
||||
})
|
||||
}).then((message)=>{
|
||||
// alert('login')
|
||||
console.log('rocket chat login successfully', message)
|
||||
this.RocketChatClientService.getRooms().then((rooms)=>{
|
||||
console.log('rooms', rooms)
|
||||
})
|
||||
}).catch((message)=>{
|
||||
console.log('rocket chat login failed', message)
|
||||
}).finally(()=>{
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
// this.RocketChatClientService.send()
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
export interface wsCallbacksParams {
|
||||
type: 'Offline' | 'Online' | 'Open' | 'Onmessage',
|
||||
funx: Function
|
||||
runOnces?: boolean
|
||||
requestId?: string
|
||||
}
|
||||
|
||||
|
||||
export interface msgQueue {
|
||||
message: object,
|
||||
requestId: string
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import { wsCallbacksParams, msgQueue } from 'src/app/models/rochet-chat-cliente-service'
|
||||
|
||||
class _RocketChatClientService {
|
||||
|
||||
connected = false
|
||||
private hasPing = false
|
||||
private firstPingFunx: Function
|
||||
isLogin = false;
|
||||
request = []
|
||||
|
||||
constructor() {}
|
||||
|
||||
@@ -25,7 +27,8 @@ class _RocketChatClientService {
|
||||
|
||||
this.ws.send(connectMessage)
|
||||
|
||||
this.ws.registerCallback('Onmessage',(message: any) => {
|
||||
this.ws.registerCallback({type:'Onmessage',funx:(message: any) => {
|
||||
|
||||
if(message.msg == "ping") {
|
||||
this.ws.send({msg:"pong"})
|
||||
if(this.hasPing == false) {
|
||||
@@ -34,11 +37,13 @@ class _RocketChatClientService {
|
||||
this.hasPing = true
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
}})
|
||||
|
||||
}
|
||||
|
||||
login(user) {
|
||||
|
||||
const requestId = uuidv4()
|
||||
|
||||
const loginRequest = {
|
||||
@@ -47,17 +52,33 @@ class _RocketChatClientService {
|
||||
id: requestId,
|
||||
params: [
|
||||
{
|
||||
"user": { "username": user.username },
|
||||
"password": {
|
||||
"digest": user.password,
|
||||
"algorithm":"sha-256"
|
||||
}
|
||||
user: { username: user.username },
|
||||
password: user.password
|
||||
}
|
||||
]
|
||||
}
|
||||
this.ws.send(loginRequest, requestId)
|
||||
|
||||
return requestId
|
||||
return new Promise((resolve, reject) => {
|
||||
this.ws.registerCallback({type:'Onmessage', requestId, runOnces: true, funx:(message)=>{
|
||||
// console.log(message.result.id, requestId)
|
||||
if(message.id == requestId) { // same request send
|
||||
if(message.result) {
|
||||
if(message.result.token) {
|
||||
this.isLogin = true
|
||||
resolve(message)
|
||||
} else {
|
||||
this.isLogin = false
|
||||
reject(message)
|
||||
}
|
||||
} else {
|
||||
this.isLogin = false
|
||||
reject(message)
|
||||
}
|
||||
}
|
||||
}})
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
logout(){}
|
||||
@@ -81,20 +102,37 @@ class _RocketChatClientService {
|
||||
return requestId;
|
||||
}
|
||||
|
||||
receive() {}
|
||||
subtribe() {}
|
||||
joinRoom(){}
|
||||
deleteMessage() {}
|
||||
createRoom() {}
|
||||
|
||||
getRooms() {
|
||||
const requestId = uuidv4()
|
||||
|
||||
const request = {
|
||||
"msg": "method",
|
||||
"method": "rooms/get",
|
||||
"id": requestId,
|
||||
"params": [ { "$date": 1480377601 } ]
|
||||
}
|
||||
this.ws.send(request, requestId)
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.ws.registerCallback({type:'Onmessage', requestId, runOnces: true, funx:(message)=>{
|
||||
if(message.result.id == requestId) { // same request send
|
||||
resolve(message)
|
||||
}
|
||||
}})
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
subscribe() {
|
||||
const requestId = uuidv4()
|
||||
var subscribeRequest = {
|
||||
"msg": "sub",
|
||||
"id": "unique-id",
|
||||
"id": requestId,
|
||||
"name": "stream-notify-room",
|
||||
"params":[
|
||||
"room-id/event",
|
||||
@@ -102,7 +140,16 @@ class _RocketChatClientService {
|
||||
]
|
||||
}
|
||||
|
||||
//this.ws.send(subscribeRequest);
|
||||
this.ws.send(subscribeRequest);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.ws.registerCallback({type:'Onmessage', requestId, runOnces: true, funx:(message)=>{
|
||||
if(message.result.id == requestId) { // same request send
|
||||
resolve(message)
|
||||
}
|
||||
}})
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private disconnect = () => {
|
||||
@@ -111,29 +158,30 @@ class _RocketChatClientService {
|
||||
|
||||
// socket class ==================================================================
|
||||
private socket!: WebSocket;
|
||||
private url = ''
|
||||
private callBacks: {
|
||||
type: 'Offline' | 'Online' | 'Open' | 'Onmessage',
|
||||
object?: string
|
||||
funx: Function
|
||||
}[] = []
|
||||
msgQueue : {
|
||||
message: object,
|
||||
requestId: string
|
||||
}[] = []
|
||||
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,
|
||||
runOnces: params.runOnces
|
||||
}
|
||||
|
||||
ws = {
|
||||
registerCallback:(type: 'Offline' | 'Online' | 'Open' | 'Onmessage', funx: Function, object = '') =>{
|
||||
this.callBacks.push({
|
||||
type,
|
||||
funx,
|
||||
object
|
||||
})
|
||||
},
|
||||
deleteCallback(){},
|
||||
connect:(url)=> {
|
||||
|
||||
this.url = url
|
||||
this.socket = new WebSocket(this.url);
|
||||
this.ws.connected = false
|
||||
this.wsUrl = url
|
||||
this.socket = new WebSocket(this.wsUrl);
|
||||
// bind function
|
||||
this.socket.onopen = this.ws.onopen;
|
||||
this.socket.onmessage = this.ws.onmessage;
|
||||
@@ -141,11 +189,10 @@ class _RocketChatClientService {
|
||||
this.socket.onerror = this.ws.onerror;
|
||||
},
|
||||
onopen:()=> {
|
||||
this.connected = true
|
||||
this.ws.connected = true
|
||||
console.log('================== welcome to socket server =====================')
|
||||
|
||||
|
||||
this.msgQueue.forEach((item, index, object) => {
|
||||
this.wsMsgQueue.forEach((item, index, object) => {
|
||||
this.ws.send(item.message, item.requestId);
|
||||
object.splice(index, 1);
|
||||
})
|
||||
@@ -153,9 +200,9 @@ class _RocketChatClientService {
|
||||
},
|
||||
send: (message: object, requestId = uuidv4()) => {
|
||||
|
||||
if (this.connected == false) { // save data to send when back online
|
||||
if (this.ws.connected == false) { // save data to send when back online
|
||||
console.log('save msgQueue')
|
||||
this.msgQueue.push({message, requestId})
|
||||
this.wsMsgQueue.push({message, requestId})
|
||||
} else {
|
||||
console.log('send rocket chat', message)
|
||||
let messageStr = JSON.stringify(message)
|
||||
@@ -168,18 +215,24 @@ class _RocketChatClientService {
|
||||
const data = JSON.parse(event.data)
|
||||
console.log('event.data', data)
|
||||
|
||||
this.callBacks.forEach((e)=>{
|
||||
if(e.type== 'Onmessage') {
|
||||
e.funx(data)
|
||||
for (const [key, value] of Object.entries(this.wsCallbacks)) {
|
||||
if(value.type== 'Onmessage') {
|
||||
value.funx(data)
|
||||
if(value.runOnces) {
|
||||
delete this.wsCallbacks[value.requestId || key]
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
onclose:(event: any)=> {
|
||||
|
||||
this.connect(this.firstPingFunx())
|
||||
|
||||
this.connected = false
|
||||
this.ws.connected = false
|
||||
console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user