44
packages/websocket/src/tomcat/client.ts
Normal file
44
packages/websocket/src/tomcat/client.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import { EventEmitter } from 'events'
|
||||
import { SocketIO } from '../socket-io/interfaces';
|
||||
|
||||
export class TomcatClient extends EventEmitter implements SocketIO.EngineSocket {
|
||||
private _id: string;
|
||||
private session: any
|
||||
|
||||
server: any;
|
||||
readyState: string;
|
||||
remoteAddress: string;
|
||||
upgraded: boolean;
|
||||
request: any;
|
||||
transport: any;
|
||||
|
||||
constructor(server: any, session: any) {
|
||||
super();
|
||||
this.server = server;
|
||||
this.readyState = 'open';
|
||||
this.remoteAddress = session + ''
|
||||
this.upgraded = true;
|
||||
this.request = {
|
||||
uri: () => {
|
||||
return session.getRequestURI() + ''
|
||||
},
|
||||
headers: () => {
|
||||
return []
|
||||
}
|
||||
};
|
||||
this.transport = null;
|
||||
|
||||
this.session = session;
|
||||
this._id = session.getId();
|
||||
}
|
||||
|
||||
get id() {
|
||||
return this._id;
|
||||
}
|
||||
send(text: string) {
|
||||
this.session.getBasicRemote().sendText(text)
|
||||
}
|
||||
close() {
|
||||
this.session.close();
|
||||
}
|
||||
}
|
1
packages/websocket/src/tomcat/constants.ts
Normal file
1
packages/websocket/src/tomcat/constants.ts
Normal file
@ -0,0 +1 @@
|
||||
export const ProxyBeanName = "webSocketServerProxy"
|
69
packages/websocket/src/tomcat/server.ts
Normal file
69
packages/websocket/src/tomcat/server.ts
Normal file
@ -0,0 +1,69 @@
|
||||
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("com.sixi.framework.scriptservice.websocket.WebSocketServerProxy")
|
||||
const ThreadPoolExecutor = Java.type('java.util.concurrent.ThreadPoolExecutor')
|
||||
const ThreadPoolTaskExecutor = Java.type('org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor')
|
||||
|
||||
interface TomcatWebSocketSession {
|
||||
getId: () => number
|
||||
}
|
||||
|
||||
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[tomcatClient.id] = tomcatClient
|
||||
this.emit(ServerEvent.connect, tomcatClient)
|
||||
},
|
||||
onMessage: (message: any, session: TomcatWebSocketSession) => {
|
||||
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: any) => {
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user