42 lines
		
	
	
	
		
			1 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			42 lines
		
	
	
	
		
			1 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
// SPDX-License-Identifier: GPL-3.0-or-later
 | 
						|
// Author: Sascha Nitsch https://contentnation.net/@grumpydevelop
 | 
						|
 | 
						|
import { WebSocketServer } from "ws";
 | 
						|
 | 
						|
class MonitorWS extends WebSocketServer {
 | 
						|
  constructor(options: any) {
 | 
						|
    super(options);
 | 
						|
    const that = this;
 | 
						|
    this.on('connection', (ws: any) => {
 | 
						|
      ws.on('error', this.errorHandler);
 | 
						|
      ws.on('disconnect', this.errorHandler);
 | 
						|
      ws.on('message', function (this: WebSocket, data: Buffer, isBinary: boolean ){
 | 
						|
        that.message(this, data, isBinary);
 | 
						|
      });
 | 
						|
      // ...
 | 
						|
      ws.send("Connected");
 | 
						|
    });
 | 
						|
  }
 | 
						|
 | 
						|
  errorHandler = (a: any,b: any, c:any) => {
 | 
						|
    console.log(a,b,c);
 | 
						|
  }
 | 
						|
 | 
						|
  broadcast = (msg: string) => {
 | 
						|
    if (this.clients === undefined) {
 | 
						|
      return;
 | 
						|
    }
 | 
						|
    this.clients.forEach((client) => {
 | 
						|
      if (client.readyState === WebSocket.OPEN) {;
 | 
						|
        client.send(msg);
 | 
						|
      }
 | 
						|
    })
 | 
						|
  }
 | 
						|
 | 
						|
  message = (ws: WebSocket, data: Buffer, isBinary:boolean) => {
 | 
						|
    //const message = data.toString();
 | 
						|
    //console.log('received monitor: ', message);
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
export { MonitorWS }
 |