110 lines
2.9 KiB
TypeScript
110 lines
2.9 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { io, Socket } from 'socket.io-client';
|
|
import { BehaviorSubject, Observable, Subject } from 'rxjs';
|
|
import { environment } from 'src/environments/environment';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class SocketToolsService {
|
|
|
|
constructor() {
|
|
// this.setup('NO-NEED-FOR-NOW');
|
|
}
|
|
private socket: Socket;
|
|
private errorSubject: Subject<string>;
|
|
errors$: Observable<string>;
|
|
|
|
private connectionSubject: Subject<boolean>;
|
|
connected$: Observable<boolean>;
|
|
setupSocket(authToken: string): void {
|
|
// this.socket = io(environment.socketURL, {
|
|
// path: '/chat/',
|
|
// reconnection: true,
|
|
// autoConnect: false,
|
|
// extraHeaders: {
|
|
// Authorization: 'Bearer ' + authToken
|
|
// }
|
|
// });
|
|
|
|
|
|
this.socket = io(environment.socketURL,{autoConnect: false ,reconnection: true});
|
|
this.connected$ = this.monitorConnection();
|
|
this.socket.connect();
|
|
|
|
// this.connected$ = this.monitorConnection();
|
|
|
|
this.socket.on('receive_message', () => {
|
|
console.log("app-taskactivities-refresh 001");
|
|
const event = new Event("app-taskactivities-refresh");
|
|
dispatchEvent(event);
|
|
});
|
|
|
|
this.socket.on('received_refreshmarket_jobs', (data) => {
|
|
console.log("received_refreshmarket_jobs 002");
|
|
debugger
|
|
});
|
|
|
|
|
|
}
|
|
|
|
// useEffect(() => {
|
|
// this.socket.on("receive_message", (data) => {
|
|
// // setSocketMsgReceived(data.message);
|
|
// //dispatch(tableReload({type:'CHATMESSAGELIST'}))
|
|
// });
|
|
// }, [this.socket]);
|
|
|
|
public joinSocketRoom(socketRoom){
|
|
this.socket.emit("join_room", socketRoom);
|
|
}
|
|
public emmitSocketEvent(socketGroup, message, socketRoom){
|
|
this.socket.emit(socketGroup, { message, socketRoom });
|
|
}
|
|
private monitorConnection(): Observable<boolean> {
|
|
console.log("********************* MONITORING-SOCKETS *******");
|
|
|
|
this.connectionSubject = new BehaviorSubject<boolean>(false);
|
|
|
|
this.socket.on('receive_message', () => {
|
|
console.log("app-taskactivities-refresh");
|
|
const event = new Event("app-taskactivities-refresh");
|
|
dispatchEvent(event);
|
|
});
|
|
|
|
this.socket.on('received_refreshmarket_jobs', (data) => {
|
|
console.log("received_refreshmarket_jobs");
|
|
// debugger
|
|
});
|
|
|
|
|
|
this.socket.on('connect', () => {
|
|
console.log("********************* MONITORING-SOCKETS ON CONNNECT *******");
|
|
// debugger;
|
|
this.connectionSubject.next(true);
|
|
});
|
|
|
|
this.socket.on('connection', () => {
|
|
this.connectionSubject.next(true);
|
|
});
|
|
|
|
this.socket.on('disconnect', () => {
|
|
this.connectionSubject.next(false);
|
|
});
|
|
|
|
this.socket.on('disconnecting', () => {
|
|
this.connectionSubject.next(false);
|
|
});
|
|
|
|
return this.connectionSubject.asObservable();
|
|
}
|
|
|
|
stop(): void {
|
|
this.socket?.disconnect();
|
|
// this.newMessagesSubject?.complete();
|
|
this.connectionSubject?.complete();
|
|
this.errorSubject?.complete();
|
|
}
|
|
|
|
}
|