|
@@ -1,4 +1,4 @@
|
|
|
-import { BehaviorSubject, Subject, buffer, bufferWhen, elementAt, filter, interval } from 'rxjs';
|
|
|
+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'
|
|
@@ -12,7 +12,6 @@ util.checkMaxHeap()
|
|
|
/* ---------------------- COMPLEX OPERATION ------------------------------ */
|
|
|
let msgPayload: Subject<any> = new Subject();
|
|
|
let consumerTrafficStatus: Subject<any> = new Subject()
|
|
|
-let bufferTrigger: BehaviorSubject<boolean> = new BehaviorSubject(false)
|
|
|
|
|
|
let mongoStorage: StorageLocation = {
|
|
|
type: `MongoDB`,
|
|
@@ -20,50 +19,25 @@ let mongoStorage: StorageLocation = {
|
|
|
}
|
|
|
msgData.loadObsData(mongoStorage, msgPayload)
|
|
|
|
|
|
-consumerTrafficStatus.subscribe((consumerHeapUsage) => {
|
|
|
- if (consumerHeapUsage >= 2) {
|
|
|
- // If consumerHeapUsage is over 2 %, it will trigger buffer
|
|
|
- bufferTrigger.next(true)
|
|
|
- } else {
|
|
|
- bufferTrigger.next(false)
|
|
|
- }
|
|
|
-})
|
|
|
-
|
|
|
-bufferTrigger.subscribe((element) => {
|
|
|
- if(element){
|
|
|
- console.log(`Heap Load Exceeded on client side. Buffering.....`)
|
|
|
- }
|
|
|
-})
|
|
|
-
|
|
|
-
|
|
|
// 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(bufferTrigger.pipe(filter(value => !value)))
|
|
|
+ buffer(notifier)
|
|
|
).subscribe((element) => {
|
|
|
console.log(`Emitting ${element.length} messages`)
|
|
|
socket.emit(`payload`, element);
|
|
|
|
|
|
});
|
|
|
|
|
|
- //Listen for messages from consumer/client
|
|
|
- socket.on(`message`, (message) => {
|
|
|
- console.log(`Received message from client: ${message}`)
|
|
|
- util.checkHeapSize()
|
|
|
- // Send a message back to the client
|
|
|
- socket.send(`${message} received!`);
|
|
|
- })
|
|
|
-
|
|
|
// Listen for the socket to be closed
|
|
|
socket.on('disconnect', () => {
|
|
|
console.log('Client/Consumer disconnected');
|
|
|
- // Need to put next(true) trigger the buffer due to disconnection from the other side.
|
|
|
- bufferTrigger.next(true)
|
|
|
subscription.unsubscribe();
|
|
|
});
|
|
|
})
|
|
@@ -79,7 +53,8 @@ function connectWebSocket() {
|
|
|
console.log(`Connected to Consumer'Server.`);
|
|
|
});
|
|
|
|
|
|
- socket.on('trafficControl', (report: number) => {
|
|
|
+ socket.on('trafficControl', (report: any) => {
|
|
|
+ console.log(report)
|
|
|
consumerTrafficStatus.next(report);
|
|
|
});
|
|
|
|