import * as http from 'http' ; import { inject , injectable } from 'inversify' ; import * as SocketIO from 'socket.io' ; import urljoin from 'url-join' ; import IConfigFile from '../../IConfigFile' ; import IConfiguration from '../../IConfiguration' ; import ILogger from '../../ILogger' ; import ILoggerModel from '../../ILoggerModel' ; import ISocketIOManageModel from './ISocketIOManageModel' ; @ injectable ( ) export default class SocketIOManageModel implements ISocketIOManageModel { private log : ILogger ; private config : IConfigFile ; private ios : SocketIO . Server [ ] = [ ] ; private callTimer : NodeJS . Timer | null = null ; private encodeProgressCallTimer : NodeJS . Timer | null = null ; constructor ( @ inject ( 'ILoggerModel' ) logger , @ inject ( 'IConfiguration' ) configuration ) { this . log = logger . getLogger ( ) ; this . config = configuration . getConfig ( ) ; } param servers public initialize ( servers ) { for ( const s of servers ) { this . ios . push ( new SocketIO . Server ( s , { path : typeof this . config . subDirectory === 'undefined' ? '/socket.io' : urljoin ( this . config . subDirectory , '/socket.io' ) , cors : { origin : '*' , } , } ) ) ; } this . log . system . info ( 'SocketIO Server has started.' ) ; } /**
     * client へ状態変更通知
     */ public notifyClient ( ) { if ( this . callTimer === null ) { this . callTimer = setTimeout ( ( ) => { this . callTimer = null ; if ( this . ios . length === 0 ) { throw new Error ( 'must call SocketIoManageModel initialize' ) ; } for ( const io of this . ios ) { io . sockets . emit ( 'updateStatus' ) ; } } , 200 ) ; } } /**
     * エンコードの進捗情報更新を通知
     */ public notifyUpdateEncodeProgress ( ) { if ( this . encodeProgressCallTimer === null ) { this . encodeProgressCallTimer = setTimeout ( ( ) => { this . encodeProgressCallTimer = null ; if ( this . ios . length === 0 ) { throw new Error ( 'must call SocketIoManageModel initialize' ) ; } for ( const io of this . ios ) { io . sockets . emit ( 'updateEncode' ) ; } } , 200 ) ; } } }