Initial commit
This commit is contained in:
78
src/WebServer/WebServer.ts
Normal file
78
src/WebServer/WebServer.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { Application } from 'express';
|
||||
import * as express from 'express';
|
||||
import { Server as SocketIOServer } from 'socket.io';
|
||||
import { createServer, Server as HTTPServer } from 'http';
|
||||
import { Main } from '../Main';
|
||||
import { DashboardRouter } from './DashboardRouter';
|
||||
import { join } from 'path';
|
||||
import { CalibrationRouter } from './CalibrationRouter';
|
||||
|
||||
const PREFIX = '[WebServer]';
|
||||
export class WebServer {
|
||||
private _Main: Main;
|
||||
|
||||
Dashboard: DashboardRouter;
|
||||
Calibration: CalibrationRouter;
|
||||
|
||||
httpServer: HTTPServer;
|
||||
app: Application;
|
||||
socket: SocketIOServer;
|
||||
|
||||
constructor(Main: Main) {
|
||||
this._Main = Main;
|
||||
this.Dashboard = new DashboardRouter(this._Main);
|
||||
this.Calibration = new CalibrationRouter(this._Main);
|
||||
|
||||
this.prepare();
|
||||
}
|
||||
|
||||
private prepare() {
|
||||
this.app = express();
|
||||
this.httpServer = createServer(this.app);
|
||||
this.socket = new SocketIOServer(this.httpServer);
|
||||
|
||||
this.app.use(
|
||||
express.static(
|
||||
join(__filename, '..', '..', '..', 'frontend', 'static')
|
||||
)
|
||||
);
|
||||
|
||||
this.app.use(this.Dashboard.Router);
|
||||
this.app.use(this.Calibration.Router);
|
||||
|
||||
this.socket.on('connection', (socket) => {
|
||||
socket.emit(
|
||||
'cameraRunnerState',
|
||||
this._Main.CameraRunner.getState()
|
||||
);
|
||||
socket.emit('unityRunnerState', this._Main.UnityRunner.getStatus());
|
||||
socket.emit(
|
||||
'unityWebSocketState',
|
||||
this._Main.UnityWebSocket.getState()
|
||||
);
|
||||
|
||||
socket.on('cameraRunner', (command: string, ...args: any[]) =>
|
||||
this._Main.CameraRunner.handle(command, ...args)
|
||||
);
|
||||
socket.on('unityRunner', (command: string, ...args: any[]) =>
|
||||
this._Main.UnityRunner.handle(command, ...args)
|
||||
);
|
||||
socket.on('unityWebSocket', (command: string, ...args: any[]) =>
|
||||
this._Main.UnityWebSocket.handle(command, ...args)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
listen() {
|
||||
return new Promise<void>((resolve) => {
|
||||
const port = this._Main.Config.webServer.port;
|
||||
this.httpServer.listen(port, () => {
|
||||
console.log(
|
||||
PREFIX,
|
||||
`Listening on port http://127.0.0.1:${port}`
|
||||
);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user