123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- import { SearchService } from "../services/query.service"
- import _ = require("lodash")
- import { Subject, interval } from "rxjs";
- import { io } from "socket.io-client";
- import { Server } from "socket.io"
- /* ---------------------- COMPLEX OPERATION ------------------------------ */
- // Check Heap size
- // const v8 = require('v8');
- // const heapStats = v8.getHeapStatistics();
- // const maxHeapSize = heapStats.heap_size_limit / (1024 * 1024); // Convert to MB
- // console.log(`Current maximum heap size: ${maxHeapSize.toFixed(2)} MB`);
- // const used = process.memoryUsage().heapUsed / 1024 / 1024;
- // const total = process.memoryUsage().heapTotal / 1024 / 1024;
- // console.log(`Heap memory usage: ${used.toFixed(2)} MB`);
- // console.log(`Total heap size: ${total.toFixed(2)} MB`);
- function checkHeapSize(): any {
- let currentHeapSize = process.memoryUsage().heapUsed / 1024 / 1024;
- let allocatedHeapSize = 512;
- let percentage = (currentHeapSize / allocatedHeapSize) * 100;
- console.log(`Consumer_! Heap currentHeapSize: ${currentHeapSize.toFixed(2)} MB. Percentage: ${percentage} %`);
- return percentage
- }
- // Create new Subject to handle incoming data from remote subscription
- let payload: Subject<any> = new Subject()
- payload.subscribe((element) => {
- console.log(`Received message from server: ${element.appData.msgId}`);
- })
- // Create new Subject to monitor and broadcast heap size
- let trafficControl: Subject<any> = new Subject()
- let intervalChecking = interval(1000)
- intervalChecking.subscribe(() => {
- trafficControl.next(checkHeapSize())
- })
- // Create a WebSocket server
- function createWebsocketServer() {
- const io = new Server({})
- // Listen for connections to the WebSocket server
- io.on(`connection`, (socket) => {
- console.log(`Connected to client`);
- // Subscribe to the subject when a client connects
- const subscription = trafficControl.subscribe((element) => {
- // Stringify heap status and send data over to connected client
- socket.emit('trafficControl', element);
- });
- socket.on(`disconnect`, () => {
- console.log(`Client Disconnected`)
- subscription.unsubscribe()
- })
- })
- io.listen(8081);
- }
- // Create a new WebSocket client
- function connectWebSocket() {
- const socket = io('http://localhost:8080')
- // Listen for the WebSocket connection to open
- socket.on(`connect`, () => {
- console.log(`Connected to publisher`)
- // Listen for messages from the server
- socket.on('message', (message) => {
- trafficControl.next(message);
- });
- })
- socket.emit('subscribe', 'traffic-status')
- socket.on('payload', (data) => {
- data.forEach(element => {
- payload.next(element)
- });
- })
- // Listen for the disconnect event
- socket.on('disconnect', () => {
- console.log('Disconnected from server');
- // Attempt to reconnect every 3 seconds
- setTimeout(() => {
- console.log('Attempting to reconnect...');
- socket.connect();
- }, 1000);
- });
- // Listen for errors
- socket.on('error', (error) => {
- console.error('Socket error:', error);
- });
- }
- createWebsocketServer()
- connectWebSocket();
|