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
+67 -65
View File
@@ -73,6 +73,9 @@ export class HomePage implements OnInit {
status: string = "";
audioName: string = "";
RocketChatClientService = RocketChatClientService
constructor(
private router: Router,
public modalCtrl: AlertController,
@@ -93,89 +96,88 @@ export class HomePage implements OnInit {
private processservice: ProcessesService,
private screenOrientation: ScreenOrientation,
private sqliteservice: SqliteService,
private RouteService: RouteService,
private RocketChatClientService: RocketChatClientService) {
private RouteService: RouteService) {
// this.RocketChatClientService.connect()
// this.RocketChatClientService.login({
// username: 'paulo.pinto@gabinetedigital.local',
// password: 'tabteste@006'
// })
this.RocketChatClientService.connect()
this.RocketChatClientService.login({
username: 'paulo.pinto@gabinetedigital.local',
password: 'tabteste@006'
})
const url = 'wss://gabinetedigitalchat.dyndns.info/websocket'
const socket = new WebSocket(url);
socket.onopen = ()=> {
// const url = 'wss://gabinetedigitalchat.dyndns.info/websocket'
// const socket = new WebSocket(url);
// socket.onopen = ()=> {
console.log('============================ welcome to rocket chat =========================================');
// console.log('============================ welcome to rocket chat =========================================');
const connectRequest = {
msg: "connect",
version: "1",
support: ["1", "pre2", "pre1"]
}
// const connectRequest = {
// msg: "connect",
// version: "1",
// support: ["1", "pre2", "pre1"]
// }
// ======================= connect
socket.send(JSON.stringify(connectRequest ))
};
// // ======================= connect
// socket.send(JSON.stringify(connectRequest ))
// };
socket.onmessage = (event: any)=> {
// socket.onmessage = (event: any)=> {
console.log('event.data', JSON.parse(event.data))
// console.log('event.data', JSON.parse(event.data))
const data = JSON.parse(event.data)
// const data = JSON.parse(event.data)
if(event.data=='{"msg":"ping"}') {
socket.send('{"msg":"pong"}')
// if(event.data=='{"msg":"ping"}') {
// socket.send('{"msg":"pong"}')
const loginRequest = {
msg: "method",
method: "login",
id: uuidv4(),
params: [
{
"user": {
"username": "paulo.pinto"
},
"password": "tabteste@006"
}
]
}
// const loginRequest = {
// msg: "method",
// method: "login",
// id: uuidv4(),
// params: [
// {
// "user": {
// "username": "paulo.pinto"
// },
// "password": "tabteste@006"
// }
// ]
// }
// ======================== login
socket.send(JSON.stringify(loginRequest))
}
// // ======================== login
// socket.send(JSON.stringify(loginRequest))
// }
// login successfully
if(data.result) {
if(data.result.token) {
console.log('login successfully')
// // login successfully
// if(data.result) {
// if(data.result.token) {
// console.log('login successfully')
const requestId = uuidv4()
// const requestId = uuidv4()
var requestMessage = {
msg: "method",
method: "sendMessage",
id: requestId,
params: [{
_id: "message-id" || uuidv4(),
rid: "room-id" || 444,
msg: "Hello World!"
}]
}
// var requestMessage = {
// msg: "method",
// method: "sendMessage",
// id: requestId,
// params: [{
// _id: "message-id" || uuidv4(),
// rid: "room-id" || 444,
// msg: "Hello World!"
// }]
// }
// socket.send(JSON.stringify(requestMessage ))
// // socket.send(JSON.stringify(requestMessage ))
}
}
};
// }
// }
// };
socket.onclose = (event: any)=> {
console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
};
// socket.onclose = (event: any)=> {
// console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
// };
socket.onerror =(event: any)=> {
console.log(`[error] ${event.message}`);
};
// socket.onerror =(event: any)=> {
// console.log(`[error] ${event.message}`);
// };
// this.RocketChatClientService.send()
-5
View File
@@ -30,7 +30,6 @@ export class AuthService {
public alertController: AlertController,
private aesencrypt: AESEncrypt,
private cookieService: CookieService,
private RocketChatClientService: RocketChatClientService
) {
this.headers = new HttpHeaders();
@@ -99,10 +98,6 @@ export class AuthService {
"password": user.password,
}
console.log(postData);
this.RocketChatClientService.login(postData)
let responseChat = await this.httpService.post('login', postData).toPromise();
if(responseChat) {
console.log('Login to Rocket chat OK');
@@ -1,16 +0,0 @@
import { TestBed } from '@angular/core/testing';
import { SocketInterfaceService } from './socket-interface.service';
describe('SocketInterfaceService', () => {
let service: SocketInterfaceService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(SocketInterfaceService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
@@ -1,85 +0,0 @@
import { Injectable } from '@angular/core';
import { v4 as uuidv4 } from 'uuid';
@Injectable({
providedIn: 'root'
})
export class SocketInterfaceService {
private socket!: WebSocket;
private url = ''
private connected = false
private callBacks: {
type: 'Offline' | 'Online' | 'Open' ,
object?: string
funx: Function
}[] = []
private msgQueue : {
message: object,
requestId: string
}[] = []
private msgQueueIds = []
private disconnect: Function
constructor() { }
registerCallback(type: 'Offline' | 'Online' | 'Open', 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.onopen;
this.socket.onmessage = this.onmessage;
this.socket.onclose = this.onclose;
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.message, item.requestId);
object.splice(index, 1);
this.msgQueueIds.slice(index, 1)
})
}
send(message: object, requestId = uuidv4()) {
if (this.connected === false) { // save data to send when back online
this.msgQueue.push({message, requestId})
this.msgQueueIds.push(requestId)
} else {
let messageStr = JSON.stringify(message)
this.socket.send(messageStr)
}
return requestId
}
onmessage(event: any) {
console.log('event.data', JSON.parse(event.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}`);
}
}
@@ -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()