UNPKG

test-bed-time-service

Version:

A time service for the test-bed, producing messages with real time, fictive time and scenario duration.

96 lines 4.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const time_service_1 = require("./time-service"); const http_1 = require("http"); const express = require("express"); const cors = require("cors"); const path = require("path"); const socketIO = require("socket.io"); const node_test_bed_adapter_1 = require("node-test-bed-adapter"); /** Main application */ class App { constructor(options) { this.port = options.port; this.app = express(); this.app.use(cors()); const pwd = path.join(process.cwd(), 'public'); this.app.use('/time-service/', express.static(pwd)); this.server = http_1.createServer(this.app); this.io = socketIO(this.server, { 'path': '/time-service/socket.io/' }); this.timeService = new time_service_1.TimeService(options); this.timeService.on('time', (time) => { // console.log(`Sending time update: ${JSON.stringify(time, null, 2)}`); this.io.emit('time', time); }); this.timeService.on('stateUpdated', (state) => { console.log(`Sending state update: ${state}`); this.io.emit('stateUpdated', state); }); this.timeService.connect().then(() => this.listen()); } listen() { this.server.listen(this.port, () => { console.log(`Running server on port ${this.port}.`); }); this.io.on('connect', (socket) => { console.log(`Connected client on port ${this.port}`); socket.emit('stateUpdated', this.timeService.state.name); socket.emit('time', this.timeService.state.createTimeMessage()); // this.timeService.sendTimeUpdate(); socket.on('message', (m) => { console.log('[server](message): %s', JSON.stringify(m)); this.io.emit('message', m); }); socket.on('init', (trialTime) => { console.log('[server](message): init request received.'); this.timeService.transition({ trialTime, command: node_test_bed_adapter_1.TimeCommand.Init, }); }); socket.on('start', (trialTimeSpeed) => { console.log(`[server](message): start request received (speed = ${trialTimeSpeed}).`); this.timeService.transition({ trialTimeSpeed, command: node_test_bed_adapter_1.TimeCommand.Start, }); }); socket.on('pause', () => { console.log('[server](message): pause request received.'); this.timeService.transition({ command: node_test_bed_adapter_1.TimeCommand.Pause, }); }); socket.on('stop', () => { console.log('[server](message): stop request received.'); this.timeService.transition({ command: node_test_bed_adapter_1.TimeCommand.Stop, }); }); socket.on('update', (trialTimeSpeed, trialTime) => { console.log('[server](message): update request received.'); this.timeService.transition({ trialTimeSpeed, trialTime, command: node_test_bed_adapter_1.TimeCommand.Update, }); }); socket.on('reset', () => { console.log('[server](message): reset request received.'); this.timeService.transition({ command: node_test_bed_adapter_1.TimeCommand.Reset, }); }); socket.on('disconnect', () => { console.log('Client disconnected'); }); }); } getApp() { return this.app; } } exports.App = App; //# sourceMappingURL=app.js.map