mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-18 20:47:54 +00:00
connect to socket server
This commit is contained in:
@@ -27,7 +27,8 @@ import { ScreenOrientation } from '@ionic-native/screen-orientation/ngx';
|
||||
import { SqliteService } from 'src/app/services/sqlite.service';
|
||||
import { Device } from '@capacitor/device';
|
||||
import { RouteService } from 'src/app/services/route.service';
|
||||
|
||||
import { RocketChatClientService } from 'src/app/services/socket/rocket-chat-client.service';
|
||||
import { environment } from 'src/environments/environment';
|
||||
|
||||
@Component({
|
||||
selector: 'app-home',
|
||||
@@ -90,7 +91,8 @@ export class HomePage implements OnInit {
|
||||
private processservice: ProcessesService,
|
||||
private screenOrientation: ScreenOrientation,
|
||||
private sqliteservice: SqliteService,
|
||||
private RouteService: RouteService) {
|
||||
private RouteService: RouteService,
|
||||
private RocketChatClientService: RocketChatClientService) {
|
||||
|
||||
/* this.webNotificationPopupService.askNotificationPermission() */
|
||||
|
||||
@@ -235,3 +237,46 @@ export class HomePage implements OnInit {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
const url = 'wss://gabinetedigitalchat.dyndns.info/websocket'
|
||||
const socket = new WebSocket(url);
|
||||
|
||||
|
||||
socket.onopen = ()=> {
|
||||
|
||||
console.log('============================ welcome to rocket chat =========================================');
|
||||
|
||||
|
||||
const loginRequest = {
|
||||
msg: "method",
|
||||
method: "login",
|
||||
id: "42",
|
||||
params: [
|
||||
{
|
||||
"user": { "username": 'paulo.pinto@gabinetedigital.local' },
|
||||
"password": {
|
||||
"digest": 'tabteste@006',
|
||||
"algorithm":"sha-256"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
// temp login
|
||||
socket.send(JSON.stringify(loginRequest))
|
||||
};
|
||||
|
||||
socket.onmessage = (event: any)=> {
|
||||
console.log('rocket chat message', event)
|
||||
console.log('event.data', JSON.parse(event.data))
|
||||
};
|
||||
|
||||
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}`);
|
||||
};
|
||||
@@ -10,6 +10,7 @@ import { AlertController } from '@ionic/angular';
|
||||
import { SessionStore } from '../store/session.service';
|
||||
import { AESEncrypt } from '../services/aesencrypt.service';
|
||||
import { CookieService } from 'ngx-cookie-service';
|
||||
import { RocketChatClientService } from '../services/socket/rocket-chat-client.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@@ -29,6 +30,7 @@ export class AuthService {
|
||||
public alertController: AlertController,
|
||||
private aesencrypt: AESEncrypt,
|
||||
private cookieService: CookieService,
|
||||
private RocketChatClientService: RocketChatClientService
|
||||
) {
|
||||
|
||||
this.headers = new HttpHeaders();
|
||||
@@ -99,8 +101,10 @@ export class AuthService {
|
||||
|
||||
console.log(postData);
|
||||
|
||||
this.RocketChatClientService.login(postData)
|
||||
|
||||
let responseChat = await this.httpService.post('login', postData).toPromise();
|
||||
if(responseChat){
|
||||
if(responseChat) {
|
||||
console.log('Login to Rocket chat OK');
|
||||
this.ValidatedUserChat = responseChat;
|
||||
localStorage.setItem('userChat', JSON.stringify(responseChat));
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
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();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class SocketInterfaceService {
|
||||
|
||||
private socket!: WebSocket;
|
||||
private url = ''
|
||||
private connected = false
|
||||
private callBacks: {
|
||||
type: 'Offline' | 'Online' | 'Onmessage' | 'Chat' | 'Notification' | 'Notifications' | '',
|
||||
object?: string
|
||||
funx: Function
|
||||
}[] = []
|
||||
private msgQueue = []
|
||||
|
||||
constructor() { }
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
send(message: object) {
|
||||
if (!this.connected) { // save data to send when back online
|
||||
this.msgQueue.push(message)
|
||||
} else {
|
||||
let messageStr = JSON.stringify(message)
|
||||
this.socket.send(messageStr)
|
||||
}
|
||||
}
|
||||
|
||||
onopen() {
|
||||
|
||||
}
|
||||
onmessage() {}
|
||||
|
||||
onclose(event: any) {
|
||||
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}`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { RocketChatClientService } from './rocket-chat-client.service';
|
||||
|
||||
describe('RocketChatClientService', () => {
|
||||
let service: RocketChatClientService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(RocketChatClientService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,46 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { SocketInterfaceService } from './interface/socket-interface.service'
|
||||
import { environment } from 'src/environments/environment';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class RocketChatClientService {
|
||||
|
||||
private SocketInterfaceService = new SocketInterfaceService()
|
||||
|
||||
constructor() {
|
||||
const url = environment.apiChatUrl.replace('https','wss')
|
||||
// this.SocketInterfaceService.connect(url);
|
||||
}
|
||||
|
||||
login(user) {
|
||||
const loginRequest = {
|
||||
msg: "method",
|
||||
method: "login",
|
||||
id: "42",
|
||||
params: [
|
||||
{
|
||||
"user": { "username": user.username },
|
||||
"password": {
|
||||
"digest": user.password,
|
||||
"algorithm":"sha-256"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
// this.SocketInterfaceService.send(loginRequest)
|
||||
}
|
||||
|
||||
logout(){ }
|
||||
|
||||
send(roomId, message, option) {}
|
||||
|
||||
receive() {}
|
||||
joinRoom(){}
|
||||
deleteMessage() {}
|
||||
|
||||
}
|
||||
|
||||
window['RocketChatClientService'] = new RocketChatClientService();
|
||||
Reference in New Issue
Block a user