|
|
|
@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* websocket配置项类
|
|
|
|
|
* author: frez02@126.com
|
|
|
|
|
* datetime: 2021-01-02
|
|
|
|
|
*/
|
|
|
|
|
export class WebSocketOption {
|
|
|
|
|
/**
|
|
|
|
|
* 是否自动启动。开启后,遇error将会自动重启连接
|
|
|
|
|
*/
|
|
|
|
|
autoStart: boolean = false
|
|
|
|
|
/**
|
|
|
|
|
* 心跳数据包
|
|
|
|
|
*/
|
|
|
|
|
heart: string | false = false
|
|
|
|
|
/**
|
|
|
|
|
* WebSocket服务地址
|
|
|
|
|
*/
|
|
|
|
|
ws: string | null = null //ws://170.0.35.145:12781/rolling/wsnotify
|
|
|
|
|
/**
|
|
|
|
|
* 连接模式
|
|
|
|
|
*/
|
|
|
|
|
type: "TCP" = "TCP"
|
|
|
|
|
|
|
|
|
|
//以下为回调函数
|
|
|
|
|
/**
|
|
|
|
|
* onopen
|
|
|
|
|
*/
|
|
|
|
|
onOpen?: Function
|
|
|
|
|
/**
|
|
|
|
|
* onmessage
|
|
|
|
|
*/
|
|
|
|
|
onMessage?: Function
|
|
|
|
|
/**
|
|
|
|
|
* onclose
|
|
|
|
|
*/
|
|
|
|
|
onClose?: Function
|
|
|
|
|
/**
|
|
|
|
|
* onerror
|
|
|
|
|
*/
|
|
|
|
|
onError?: Function
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* websocket类
|
|
|
|
|
* author: frez02@126.com
|
|
|
|
|
* datetime: 2020-01-02
|
|
|
|
|
*/
|
|
|
|
|
export class WebSocketLoader {
|
|
|
|
|
protected server : WebSocket | null = null
|
|
|
|
|
protected option : WebSocketOption
|
|
|
|
|
protected disposed : boolean = false
|
|
|
|
|
protected paused : boolean = false
|
|
|
|
|
|
|
|
|
|
constructor(opts : WebSocketOption) {
|
|
|
|
|
this.option = opts
|
|
|
|
|
|
|
|
|
|
if(opts.autoStart) {
|
|
|
|
|
this.start()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 开启连接
|
|
|
|
|
*/
|
|
|
|
|
start = () => {
|
|
|
|
|
this.restart()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 重新开始连接
|
|
|
|
|
*/
|
|
|
|
|
restart = () => {
|
|
|
|
|
this.disposed = false
|
|
|
|
|
this.paused = false
|
|
|
|
|
this.connect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 暂停接收
|
|
|
|
|
*/
|
|
|
|
|
pause = () => {
|
|
|
|
|
this.paused = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 恢复接收
|
|
|
|
|
*/
|
|
|
|
|
resume = () => {
|
|
|
|
|
this.paused = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected connect = () => {
|
|
|
|
|
if(this.server == null){
|
|
|
|
|
this.server = new WebSocket(this.option.ws)
|
|
|
|
|
}
|
|
|
|
|
else if(this.server.readyState == 1){
|
|
|
|
|
this.server.close()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
else if(this.server.readyState == 2){
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
//0 - 尚未建立连接, 3 - 已经关闭或者已经不能打开
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.server.onclose = this.onClose
|
|
|
|
|
this.server.onopen = this.onOpen
|
|
|
|
|
this.server.onmessage = this.onMessage
|
|
|
|
|
this.server.onerror = this.onError
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 释放连接
|
|
|
|
|
*/
|
|
|
|
|
dispose = () => {
|
|
|
|
|
this.disposed = true
|
|
|
|
|
|
|
|
|
|
if(this.server == null){
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(this.server.readyState == 2) {
|
|
|
|
|
this.server.close()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 连接是否活动状态
|
|
|
|
|
*/
|
|
|
|
|
isLive = () => {
|
|
|
|
|
return this.server == null ? false : (this.disposed == true ? false : this.server.readyState == 1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected onOpen = (e : any) => {
|
|
|
|
|
if(this.option.onOpen){
|
|
|
|
|
this.option.onOpen()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected onClose = (e : any) => {
|
|
|
|
|
if(this.option.onClose){
|
|
|
|
|
this.option.onClose()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected onMessage = (e : any) => {
|
|
|
|
|
if(this.option.onMessage){
|
|
|
|
|
this.option.onMessage(e.data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected onError = (e : any) => {
|
|
|
|
|
if(this.option.autoStart && this.disposed == false){
|
|
|
|
|
this.restart()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(this.option.onError){
|
|
|
|
|
this.option.onError()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|