1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- import { Subject } from 'rxjs';
- import * as WebSocket from 'ws';
- import { DataPrepService } from '../services/dataprep.service';
- import { StorageLocation } from '../types/interface';
- let msgData = new DataPrepService()
- /* ---------------------- COMPLEX OPERATION ------------------------------ */
- const msgPayload: 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
- const wss = new WebSocket.Server({ port: 8080 });
- // Listen for connections to the WebSocket server
- wss.on('connection', (ws: WebSocket) => {
- console.log('Client connected');
- // Subscribe to the subject when a client connects
- const subscription = msgPayload.subscribe((element) => {
- 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`);
- console.log(`Emitting value ${element.appData.msgId} to client`);
- const messageString = JSON.stringify(element);
- ws.send(messageString);
- });
- // Listen for messages from the client
- ws.on('message', (message: any) => {
- console.log(`Received message from client: ${message}`);
- });
- // Unsubscribe from the subject when the client disconnects
- ws.on('close', () => {
- console.log('Client disconnected');
- subscription.unsubscribe();
- });
- });
|