import { inject , injectable } from 'inversify' ; import * as socketIo from 'socket.io-client' ; import Util from '../../util/Util' ; import IServerConfigModel from '../serverConfig/IServerConfigModel' ; import ISocketIOModel from './ISocketIOModel' ; @ injectable ( ) class SocketIOModel implements ISocketIOModel { private serverConfiModel : IServerConfigModel ; private io : socketIo . Socket | null = null ; constructor ( @ inject ( 'IServerConfigModel' ) serverConfiModel ) { this . serverConfiModel = serverConfiModel ; } /**
     * SokcetIO 初期設定
     */ public Iinitialize ( ) { const config = this . serverConfiModel . getConfig ( ) ; if ( config === null || this . io !== null ) { throw new Error ( 'InitializationSocketIOError' ) ; } this . io = socketIo . io ( `${ location . protocol }//${ location . hostname }:${ config . socketIOPort }` , { path : `${ Util . getSubDirectory ( ) }/socket.io` , } ) ; } /**
     * 設定済み socketIO をのインスタを返す
     */ public getIO ( ) { return this . io ; } param callback public onUpdateState ( callback ) { if ( this . io === null ) { throw new Error ( 'IOIsNull' ) ; } this . io . on ( SocketIOModel . UPDATE_STATUS_EVENT , callback ) ; } param callback public offUpdateState ( callback ) { if ( this . io === null ) { throw new Error ( 'IOIsNull' ) ; } this . io . off ( SocketIOModel . UPDATE_STATUS_EVENT , callback ) ; } param callback public onUpdateEncodeState ( callback ) { if ( this . io === null ) { throw new Error ( 'IOIsNull' ) ; } this . io . on ( SocketIOModel . UPDATE_ENCODE_STATUS_EVENT , callback ) ; } param callback public offUpdateEncodeState ( callback ) { if ( this . io === null ) { throw new Error ( 'IOIsNull' ) ; } this . io . off ( SocketIOModel . UPDATE_ENCODE_STATUS_EVENT , callback ) ; } } namespace SocketIOModel { export const UPDATE_STATUS_EVENT = 'updateStatus' ; export const UPDATE_ENCODE_STATUS_EVENT = 'updateEncode' ; } export default SocketIOModel ;