mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 13:02:56 +00:00
Merge branch 'feature/websocket' of https://bitbucket.org/equilibriumito/gabinete-digital into feature/websocket
This commit is contained in:
+10
-83
@@ -72,6 +72,9 @@ export class HomePage implements OnInit {
|
||||
|
||||
status: string = "";
|
||||
audioName: string = "";
|
||||
|
||||
RocketChatClientService = RocketChatClientService
|
||||
|
||||
constructor(
|
||||
private router: Router,
|
||||
public modalCtrl: AlertController,
|
||||
@@ -92,90 +95,14 @@ export class HomePage implements OnInit {
|
||||
private processservice: ProcessesService,
|
||||
private screenOrientation: ScreenOrientation,
|
||||
private sqliteservice: SqliteService,
|
||||
private RouteService: RouteService,
|
||||
private RocketChatClientService: RocketChatClientService) {
|
||||
|
||||
// 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 = ()=> {
|
||||
|
||||
console.log('============================ welcome to rocket chat =========================================');
|
||||
|
||||
const connectRequest = {
|
||||
msg: "connect",
|
||||
version: "1",
|
||||
support: ["1", "pre2", "pre1"]
|
||||
}
|
||||
|
||||
// ======================= connect
|
||||
socket.send(JSON.stringify(connectRequest ))
|
||||
};
|
||||
|
||||
socket.onmessage = (event: any)=> {
|
||||
|
||||
console.log('event.data', JSON.parse(event.data))
|
||||
|
||||
const data = JSON.parse(event.data)
|
||||
|
||||
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"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
// ======================== login
|
||||
socket.send(JSON.stringify(loginRequest))
|
||||
}
|
||||
|
||||
// login successfully
|
||||
if(data.result) {
|
||||
if(data.result.token) {
|
||||
console.log('login successfully')
|
||||
|
||||
const requestId = uuidv4()
|
||||
|
||||
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.onclose = (event: any)=> {
|
||||
console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
|
||||
};
|
||||
|
||||
socket.onerror =(event: any)=> {
|
||||
console.log(`[error] ${event.message}`);
|
||||
};
|
||||
private RouteService: RouteService) {
|
||||
|
||||
this.RocketChatClientService.connect(()=>{
|
||||
this.RocketChatClientService.login({
|
||||
username: 'paulo.pinto@gabinetedigital.local',
|
||||
password: 'tabteste@006'
|
||||
})
|
||||
})
|
||||
|
||||
// this.RocketChatClientService.send()
|
||||
/* this.webNotificationPopupService.askNotificationPermission() */
|
||||
|
||||
@@ -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,38 @@
|
||||
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
|
||||
hasPing = false
|
||||
|
||||
constructor() {
|
||||
|
||||
}
|
||||
constructor() {}
|
||||
|
||||
private returns() {}
|
||||
|
||||
connect(url = 'wss://gabinetedigitalchat.dyndns.info/websocket') {
|
||||
connect(firstPing: Function) {
|
||||
|
||||
this.SocketInterfaceService.connect(url);
|
||||
this.ws.connect('wss://gabinetedigitalchat.dyndns.info/websocket');
|
||||
|
||||
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"})
|
||||
if(this.hasPing == false) {
|
||||
// first ping
|
||||
firstPing()
|
||||
this.hasPing = true
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
login(user) {
|
||||
@@ -45,12 +52,12 @@ export class RocketChatClientService {
|
||||
}
|
||||
]
|
||||
}
|
||||
this.SocketInterfaceService.send(loginRequest, requestId)
|
||||
this.ws.send(loginRequest, requestId)
|
||||
|
||||
return requestId
|
||||
}
|
||||
|
||||
logout(){ }
|
||||
logout(){}
|
||||
|
||||
send(roomId, message, option) {
|
||||
const requestId = uuidv4()
|
||||
@@ -66,7 +73,7 @@ export class RocketChatClientService {
|
||||
}]
|
||||
}
|
||||
|
||||
this.SocketInterfaceService.send(request, requestId);
|
||||
// this.ws.send(request, requestId);
|
||||
|
||||
return requestId;
|
||||
}
|
||||
@@ -87,13 +94,94 @@ export class RocketChatClientService {
|
||||
]
|
||||
}
|
||||
|
||||
this.SocketInterfaceService.send(subscribeRequest);
|
||||
//this.ws.send(subscribeRequest);
|
||||
}
|
||||
|
||||
private disconnect = () => {
|
||||
|
||||
}
|
||||
|
||||
// socket class ==================================================================
|
||||
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()
|
||||
Reference in New Issue
Block a user