|
@@ -3,38 +3,23 @@ import _ = require("lodash")
|
|
|
import { Subject, interval } from "rxjs";
|
|
|
import { io } from "socket.io-client";
|
|
|
import { Server } from "socket.io"
|
|
|
+import { UtilityService } from "../services/utility.service";
|
|
|
|
|
|
+const util = new UtilityService()
|
|
|
+util.checkMaxHeap()
|
|
|
/* ---------------------- 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}`);
|
|
|
+ // 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)
|
|
|
+let intervalChecking = interval(5000)
|
|
|
intervalChecking.subscribe(() => {
|
|
|
- trafficControl.next(checkHeapSize())
|
|
|
+ trafficControl.next(util.checkHeapSize())
|
|
|
})
|
|
|
|
|
|
// Create a WebSocket server
|
|
@@ -42,21 +27,21 @@ function createWebsocketServer() {
|
|
|
const io = new Server({})
|
|
|
// Listen for connections to the WebSocket server
|
|
|
io.on(`connection`, (socket) => {
|
|
|
- console.log(`Connected to client`);
|
|
|
+ console.log(`Connected to clients`);
|
|
|
|
|
|
// 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`)
|
|
|
+ console.log(`Clients Disconnected`)
|
|
|
subscription.unsubscribe()
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
+ // create localhost server port 8080
|
|
|
io.listen(8081);
|
|
|
}
|
|
|
|
|
@@ -67,7 +52,7 @@ function connectWebSocket() {
|
|
|
|
|
|
// Listen for the WebSocket connection to open
|
|
|
socket.on(`connect`, () => {
|
|
|
- console.log(`Connected to publisher`)
|
|
|
+ console.log(`Connected to publisher'server`)
|
|
|
|
|
|
|
|
|
// Listen for messages from the server
|
|
@@ -76,9 +61,9 @@ function connectWebSocket() {
|
|
|
});
|
|
|
})
|
|
|
|
|
|
- socket.emit('subscribe', 'traffic-status')
|
|
|
-
|
|
|
- socket.on('payload', (data) => {
|
|
|
+ // Receive payload from publisher and push them into local Subject
|
|
|
+ socket.on('payload', (data: any[]) => {
|
|
|
+ console.log(data.length)
|
|
|
data.forEach(element => {
|
|
|
payload.next(element)
|
|
|
});
|
|
@@ -86,7 +71,7 @@ function connectWebSocket() {
|
|
|
|
|
|
// Listen for the disconnect event
|
|
|
socket.on('disconnect', () => {
|
|
|
- console.log('Disconnected from server');
|
|
|
+ console.log(`Disconnected from publisher'server`);
|
|
|
|
|
|
// Attempt to reconnect every 3 seconds
|
|
|
setTimeout(() => {
|