Skip to content

Commit

Permalink
断线重连
Browse files Browse the repository at this point in the history
  • Loading branch information
lzghzr committed Nov 10, 2018
1 parent dbcfd39 commit eef5145
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
4 changes: 2 additions & 2 deletions bilive/client_re.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ClientRE extends Client {
private _ClientReConnect() {
if (this._update) this._update = false
else {
setTimeout(() => {
this._Timer = setTimeout(() => {
if (this.reConnectTime >= 5) {
this.reConnectTime = 0
this._DelayReConnect()
Expand All @@ -71,7 +71,7 @@ class ClientRE extends Client {
* @memberof ClientRE
*/
private _DelayReConnect() {
setTimeout(() => this.Connect(), 5 * 60 * 1000)
this._Timer = setTimeout(() => this.Connect(), 5 * 60 * 1000)
tools.ErrorLog('尝试重连服务器失败,五分钟后再次重新连接')
}
}
Expand Down
45 changes: 43 additions & 2 deletions bilive/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,55 @@ class Client extends EventEmitter {
* @memberof Client
*/
protected _wsClient!: ws
/**
* 是否已经连接到服务器
*
* @protected
* @type {boolean}
* @memberof Client
*/
protected _connected: boolean = false
/**
* 全局计时器, 负责除心跳超时的其他任务, 便于停止
*
* @protected
* @type {NodeJS.Timer}
* @memberof Client
*/
protected _Timer!: NodeJS.Timer
/**
* 心跳超时
*
* @protected
* @type {NodeJS.Timer}
* @memberof Client
*/
protected _timeout!: NodeJS.Timer
/**
* 连接到指定服务器
*
* @memberof Client
*/
public Connect() {
if (this._wsClient !== undefined && this._wsClient.readyState === ws.OPEN) return
if (this._connected) return
this._connected = true
this._wsClient = new ws(this._server, [this._protocol])
this._wsClient
.on('error', error => this._ClientErrorHandler(error))
.on('close', () => this.Close())
.on('message', (data: string) => this._MessageHandler(data))
.on('ping', () => this._PingHandler())
this._PingHandler()
}
/**
* 断开与服务器的连接
*
* @memberof Client
*/
public Close() {
if (this._wsClient === undefined || this._wsClient.readyState !== ws.OPEN) return
if (!this._connected) return
this._connected = false
clearTimeout(this._timeout)
this._wsClient.close()
this._wsClient.terminate()
this._wsClient.removeAllListeners()
Expand All @@ -80,6 +109,18 @@ class Client extends EventEmitter {
this.emit('clientError', error)
this.Close()
}
/**
* ping/pong
*
* @protected
* @memberof Client
*/
protected _PingHandler() {
clearTimeout(this._timeout)
this._timeout = setTimeout(() => {
this._ClientErrorHandler(Error('timeout'))
}, 2 * 60 * 1000)
}
/**
* 解析消息
*
Expand Down
4 changes: 3 additions & 1 deletion bilive/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class Listener extends EventEmitter {
* @memberof Listener
*/
private _lastUpdate: number = Date.now()
// @ts-ignore
private _loop: NodeJS.Timer
/**
* 开始监听
*
Expand All @@ -79,7 +81,7 @@ class Listener extends EventEmitter {
public Start() {
this.updateAreaRoom()
// 3s清空一次消息缓存
setInterval(() => this._MSGCache.clear(), 3 * 1000)
this._loop = setInterval(() => this._MSGCache.clear(), 3 * 1000)
const { 0: server, 1: protocol } = Options._.config.serverURL.split('#')
if (protocol !== undefined && protocol !== '') this._RoomListener(server, protocol)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bilive_client",
"version": "1.4.0",
"version": "1.4.1",
"description": "基于Node.JS的bilibili直播挂机系统",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit eef5145

Please sign in to comment.