import { Plugin, Configurable, EventListener, DependencyLoad } from '@squeebot/core/lib/plugin'; import express from 'express'; import http from 'http'; import path from 'path'; import * as WebSocket from 'ws'; import { logger } from '@squeebot/core/lib/core'; import { EventEmitter } from 'events'; class WebSocketServer extends EventEmitter { public app = express(); public server = http.createServer(this.app); public wss = new WebSocket.Server({ server: this.server }); public running = false; constructor( public port: number, public host: string) { super(); } public init(): void { this.running = true; this.server.listen(this.port, this.host, () => logger.log('Web server listening on %s:%s', this.host, this.port)); } public destroy(): void { this.running = false; this.wss.close(); this.server.close(); } } class WebfaceServer extends WebSocketServer { constructor( public port: number, public host: string) { super(port, host); this.app.use(express.static(path.join(__dirname, 'public'))); this.wss.on('connection', (ws) => { }); } } @Configurable({ port: 3000, host: 'localhost' }) class WebfacePlugin extends Plugin { private srv?: WebfaceServer; @EventListener('pluginUnload') public unloadEventHandler(plugin: string | Plugin): void { if (plugin === this.name || plugin === this) { this.srv?.destroy(); this.config.save().then(() => this.emit('pluginUnloaded', this)); } } initialize(): void { this.srv = new WebfaceServer( this.config.get('port') as number, this.config.get('host')); this.srv.init(); } } module.exports = WebfacePlugin;