|
@@ -1,7 +1,9 @@
|
|
-import { BehaviorSubject, Subject, buffer, bufferWhen, elementAt, filter } from 'rxjs';
|
|
|
|
-import * as WebSocket from 'ws';
|
|
|
|
|
|
+import { BehaviorSubject, Subject, buffer, bufferWhen, elementAt, filter, interval } from 'rxjs';
|
|
import { DataPrepService } from '../services/dataprep.service';
|
|
import { DataPrepService } from '../services/dataprep.service';
|
|
import { StorageLocation } from '../types/interface';
|
|
import { StorageLocation } from '../types/interface';
|
|
|
|
+import { Server } from 'socket.io'
|
|
|
|
+import { io } from 'socket.io-client';
|
|
|
|
+
|
|
let msgData = new DataPrepService()
|
|
let msgData = new DataPrepService()
|
|
|
|
|
|
/* ---------------------- COMPLEX OPERATION ------------------------------ */
|
|
/* ---------------------- COMPLEX OPERATION ------------------------------ */
|
|
@@ -16,10 +18,10 @@ let mongoStorage: StorageLocation = {
|
|
msgData.loadObsData(mongoStorage, msgPayload)
|
|
msgData.loadObsData(mongoStorage, msgPayload)
|
|
|
|
|
|
consumerTrafficStatus.subscribe((element) => {
|
|
consumerTrafficStatus.subscribe((element) => {
|
|
- if (element >= 1.5) {
|
|
|
|
|
|
+ if (element >= 2) {
|
|
let warning: boolean = false
|
|
let warning: boolean = false
|
|
bufferTrigger.next(warning)
|
|
bufferTrigger.next(warning)
|
|
- console.log(`Buffering.....`)
|
|
|
|
|
|
+ console.log(`Heap Load Exceeded on client side. Buffering.....`)
|
|
} else {
|
|
} else {
|
|
let warning: boolean = true
|
|
let warning: boolean = true
|
|
bufferTrigger.next(warning)
|
|
bufferTrigger.next(warning)
|
|
@@ -30,65 +32,60 @@ consumerTrafficStatus.subscribe((element) => {
|
|
|
|
|
|
// Create a WebSocket server
|
|
// Create a WebSocket server
|
|
function createWebsocketServer() {
|
|
function createWebsocketServer() {
|
|
- const wss = new WebSocket.Server({ port: 8080 });
|
|
|
|
-
|
|
|
|
- // Listen for connections to the WebSocket server
|
|
|
|
- wss.on('connection', (ws: WebSocket) => {
|
|
|
|
- console.log('Client connected');
|
|
|
|
|
|
+ const io = new Server({})
|
|
|
|
+ io.on('connection', (socket) => {
|
|
|
|
+ console.log(`Consumer Connected`)
|
|
|
|
|
|
// Subscribe to the subject when a client connects
|
|
// Subscribe to the subject when a client connects
|
|
const subscription = msgPayload.pipe(
|
|
const subscription = msgPayload.pipe(
|
|
buffer(bufferTrigger.pipe(filter(Boolean)))
|
|
buffer(bufferTrigger.pipe(filter(Boolean)))
|
|
).subscribe((element) => {
|
|
).subscribe((element) => {
|
|
console.log(`Emitting ${element.length} messages`)
|
|
console.log(`Emitting ${element.length} messages`)
|
|
- let messageString = JSON.stringify(element)
|
|
|
|
- ws.send(messageString);
|
|
|
|
|
|
+ // let messageString = JSON.stringify(element)
|
|
|
|
+ socket.emit(`payload`, element);
|
|
});
|
|
});
|
|
|
|
|
|
- // Listen for messages from the client
|
|
|
|
- ws.on('message', (message: any) => {
|
|
|
|
- console.log(`Received message from client: ${message}`);
|
|
|
|
- });
|
|
|
|
|
|
+ //Listen for messages from consumer/client
|
|
|
|
+ socket.on(`message`, (message) => {
|
|
|
|
+ console.log(`Received message from client: ${message}`)
|
|
|
|
+
|
|
|
|
+ // Send a message back to the client
|
|
|
|
+ socket.send(`${message} received!`);
|
|
|
|
+ })
|
|
|
|
|
|
- // Unsubscribe from the subject when the client disconnects
|
|
|
|
- ws.on('close', () => {
|
|
|
|
|
|
+ // Listen for the socket to be closed
|
|
|
|
+ socket.on('disconnect', () => {
|
|
console.log('Client disconnected');
|
|
console.log('Client disconnected');
|
|
subscription.unsubscribe();
|
|
subscription.unsubscribe();
|
|
|
|
+ consumerTrafficStatus.next(false)
|
|
});
|
|
});
|
|
- });
|
|
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ io.listen(8080)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a new WebSocket client
|
|
// Create a new WebSocket client
|
|
function connectWebSocket() {
|
|
function connectWebSocket() {
|
|
- // Create a new WebSocket client
|
|
|
|
- const ws = new WebSocket('ws://localhost:8081');
|
|
|
|
|
|
+ const socket = io('http://localhost:8081');
|
|
|
|
|
|
- // Listen for the WebSocket connection to open
|
|
|
|
- ws.on('open', () => {
|
|
|
|
- console.log('Connecting to Consumer WebSocket server');
|
|
|
|
-
|
|
|
|
- // Send a message to the server
|
|
|
|
- ws.send('Hello, consumer server!');
|
|
|
|
- })
|
|
|
|
-
|
|
|
|
- // Listen for messages from the server
|
|
|
|
- ws.on('message', (message: string) => {
|
|
|
|
- let msgObj: number = JSON.parse(message)
|
|
|
|
- consumerTrafficStatus.next(msgObj)
|
|
|
|
|
|
+ socket.on('connect', () => {
|
|
|
|
+ console.log('Connected to publisher');
|
|
});
|
|
});
|
|
|
|
|
|
- // Listen for WebSocket errors
|
|
|
|
- ws.on('error', (element) => {
|
|
|
|
- console.error('WebSocket error:', element);
|
|
|
|
- })
|
|
|
|
|
|
+ socket.on('trafficControl', (report) => {
|
|
|
|
+ consumerTrafficStatus.next(report);
|
|
|
|
+ });
|
|
|
|
|
|
|
|
+ socket.on('disconnect', () => {
|
|
|
|
+ console.log('Disconnected from server');
|
|
|
|
|
|
- // Listen for the WebSocket connection to close
|
|
|
|
- ws.on('close', () => {
|
|
|
|
- console.log('Disconnected from WebSocket server');
|
|
|
|
- setTimeout(connectWebSocket, 1000); // Attempt to reconnect after 1 second
|
|
|
|
- })
|
|
|
|
|
|
+ // Attempt to reconnect every 3 seconds
|
|
|
|
+ setTimeout(() => {
|
|
|
|
+ console.log('Attempting to reconnect...');
|
|
|
|
+ socket.connect();
|
|
|
|
+ }, 3000);
|
|
|
|
+ });
|
|
}
|
|
}
|
|
|
|
|
|
createWebsocketServer()
|
|
createWebsocketServer()
|