66 lines
2.7 KiB
TypeScript
66 lines
2.7 KiB
TypeScript
import { EventEmitter } from 'events'
|
|
|
|
import { ServerOptions } from '../socket-io'
|
|
import { ServerEvent } from '../socket-io/constants'
|
|
import { SocketIO } from '../socket-io/interfaces'
|
|
import { ProxyBeanName } from './constants'
|
|
import { TomcatClient } from './client'
|
|
|
|
const WebSocketServerProxy = Java.type("pw.yumc.MiaoScript.websocket.WebSocketProxy")
|
|
const ThreadPoolExecutor = Java.type('java.util.concurrent.ThreadPoolExecutor')
|
|
const ThreadPoolTaskExecutor = Java.type('org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor')
|
|
|
|
type TomcatWebSocketSession = javax.websocket.Session
|
|
|
|
class TomcatWebSocketServer extends EventEmitter {
|
|
private beanFactory: any
|
|
private executor: any
|
|
private allClients: { [key: string]: SocketIO.EngineSocket }
|
|
|
|
constructor(beanFactory: any, options: ServerOptions) {
|
|
super()
|
|
this.allClients = {}
|
|
this.beanFactory = beanFactory
|
|
this.initThreadPool()
|
|
try { this.beanFactory.destroySingleton(ProxyBeanName) } catch (error) { }
|
|
let NashornWebSocketServerProxy = Java.extend(WebSocketServerProxy, {
|
|
onOpen: (session: TomcatWebSocketSession) => {
|
|
let tomcatClient = new TomcatClient(this, session)
|
|
this.allClients[session.getId()] = tomcatClient
|
|
this.emit(ServerEvent.connect, tomcatClient)
|
|
},
|
|
onMessage: (session: TomcatWebSocketSession, message: string) => {
|
|
this.executor.execute(() => this.emit(ServerEvent.message, this.allClients[session.getId()], message))
|
|
},
|
|
onClose: (session: TomcatWebSocketSession, reason: any) => {
|
|
this.emit(ServerEvent.disconnect, this.allClients[session.getId()], reason)
|
|
},
|
|
onError: (session: TomcatWebSocketSession, error: Error) => {
|
|
this.emit(ServerEvent.error, this.allClients[session.getId()], error)
|
|
},
|
|
})
|
|
this.beanFactory.registerSingleton(ProxyBeanName, new NashornWebSocketServerProxy())
|
|
}
|
|
private initThreadPool() {
|
|
this.executor = new ThreadPoolTaskExecutor()
|
|
this.executor.setCorePoolSize(10)
|
|
this.executor.setMaxPoolSize(100)
|
|
this.executor.setQueueCapacity(500)
|
|
this.executor.setKeepAliveSeconds(60)
|
|
this.executor.setThreadNamePrefix("@ccms/websocket-")
|
|
this.executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy())
|
|
this.executor.initialize()
|
|
}
|
|
close() {
|
|
Object.values(this.allClients).forEach(client => client.close())
|
|
this.beanFactory.destroySingleton(ProxyBeanName)
|
|
this.executor.shutdown()
|
|
}
|
|
}
|
|
|
|
export {
|
|
TomcatWebSocketServer,
|
|
ServerEvent,
|
|
TomcatClient
|
|
}
|