You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					73 lines
				
				1.6 KiB
			
		
		
			
		
	
	
					73 lines
				
				1.6 KiB
			| 
								 
											4 years ago
										 
									 | 
							
								'use strict';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* eslint-disable
							 | 
						||
| 
								 | 
							
								  class-methods-use-this
							 | 
						||
| 
								 | 
							
								*/
							 | 
						||
| 
								 | 
							
								const ws = require('ws');
							 | 
						||
| 
								 | 
							
								const BaseServer = require('./BaseServer');
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								module.exports = class WebsocketServer extends BaseServer {
							 | 
						||
| 
								 | 
							
								  constructor(server) {
							 | 
						||
| 
								 | 
							
								    super(server);
							 | 
						||
| 
								 | 
							
								    this.wsServer = new ws.Server({
							 | 
						||
| 
								 | 
							
								      noServer: true,
							 | 
						||
| 
								 | 
							
								      path: this.server.sockPath,
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    this.server.listeningApp.on('upgrade', (req, sock, head) => {
							 | 
						||
| 
								 | 
							
								      if (!this.wsServer.shouldHandle(req)) {
							 | 
						||
| 
								 | 
							
								        return;
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      this.wsServer.handleUpgrade(req, sock, head, (connection) => {
							 | 
						||
| 
								 | 
							
								        this.wsServer.emit('connection', connection, req);
							 | 
						||
| 
								 | 
							
								      });
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    this.wsServer.on('error', (err) => {
							 | 
						||
| 
								 | 
							
								      this.server.log.error(err.message);
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    const noop = () => {};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    setInterval(() => {
							 | 
						||
| 
								 | 
							
								      this.wsServer.clients.forEach((socket) => {
							 | 
						||
| 
								 | 
							
								        if (socket.isAlive === false) {
							 | 
						||
| 
								 | 
							
								          return socket.terminate();
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        socket.isAlive = false;
							 | 
						||
| 
								 | 
							
								        socket.ping(noop);
							 | 
						||
| 
								 | 
							
								      });
							 | 
						||
| 
								 | 
							
								    }, this.server.heartbeatInterval);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  send(connection, message) {
							 | 
						||
| 
								 | 
							
								    // prevent cases where the server is trying to send data while connection is closing
							 | 
						||
| 
								 | 
							
								    if (connection.readyState !== 1) {
							 | 
						||
| 
								 | 
							
								      return;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    connection.send(message);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  close(connection) {
							 | 
						||
| 
								 | 
							
								    connection.close();
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  // f should be passed the resulting connection and the connection headers
							 | 
						||
| 
								 | 
							
								  onConnection(f) {
							 | 
						||
| 
								 | 
							
								    this.wsServer.on('connection', (connection, req) => {
							 | 
						||
| 
								 | 
							
								      connection.isAlive = true;
							 | 
						||
| 
								 | 
							
								      connection.on('pong', () => {
							 | 
						||
| 
								 | 
							
								        connection.isAlive = true;
							 | 
						||
| 
								 | 
							
								      });
							 | 
						||
| 
								 | 
							
								      f(connection, req.headers);
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  onConnectionClose(connection, f) {
							 | 
						||
| 
								 | 
							
								    connection.on('close', f);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								};
							 |