12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- import { BehaviorSubject, Subject, buffer, bufferWhen, elementAt, filter, interval, map } from 'rxjs';
- import { DataPrepService } from '../services/dataprep.service';
- import { StorageLocation } from '../types/interface';
- import { Server } from 'socket.io'
- import { io } from 'socket.io-client';
- import { UtilityService } from '../services/utility.service';
- let msgData = new DataPrepService()
- let util = new UtilityService()
- util.checkMaxHeap()
- /* ---------------------- COMPLEX OPERATION ------------------------------ */
- let msgPayload: Subject<any> = new Subject();
- let consumerTrafficStatus: Subject<any> = new Subject()
- let mongoStorage: StorageLocation = {
- type: `MongoDB`,
- url: `mongodb://192.168.100.59:27017/default`
- }
- msgData.loadObsData(mongoStorage, msgPayload)
- // Create a WebSocket server
- function createWebsocketServer() {
- const io = new Server({})
- io.on('connection', (socket) => {
- console.log(`Connected to Clients/Consumers`)
- let notifier = consumerTrafficStatus.pipe(filter(value => !value.pause))
- // Subscribe to the subject when a client connects
- const subscription = msgPayload.pipe(
- buffer(notifier)
- ).subscribe((element) => {
- console.log(`Emitting ${element.length} messages`)
- socket.emit(`payload`, element);
- });
- // Listen for the socket to be closed
- socket.on('disconnect', () => {
- console.log('Client/Consumer disconnected');
- subscription.unsubscribe();
- });
- })
- io.listen(8080)
- }
- // Create a new WebSocket client
- function connectWebSocket() {
- const socket = io('http://localhost:8081');
- socket.on('connect', () => {
- console.log(`Connected to Consumer'Server.`);
- });
- socket.on('trafficControl', (report: any) => {
- console.log(report)
- consumerTrafficStatus.next(report);
- });
- socket.on('disconnect', () => {
- console.log(`Disconnected from Consumer'Server`);
- // Attempt to reconnect every 3 seconds
- setTimeout(() => {
- console.log('Attempting to reconnect...');
- socket.connect();
- }, 3000);
- });
- }
- createWebsocketServer()
- connectWebSocket();
|