Skip to content

Commit

Permalink
fixbug
Browse files Browse the repository at this point in the history
  • Loading branch information
HadiChen committed Jun 8, 2017
1 parent 8214000 commit 0ddc907
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 79 deletions.
24 changes: 24 additions & 0 deletions lib/MessageEventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ const logger = require('./logger.js')
class MessageEventEmitter extends EventEmitter {
constructor (gwcid) {
super()
this.isConstructored = true
this.gwcid = gwcid
this.processRequest = Object.create(null)
}
isWsOpen () {
if (!this.isConstructored) {
return false
}
return this.ws && this.ws.readyState === WebSocket.OPEN
}
// 收到消息的时候
onMessage (body) {
if (!this.isConstructored) {
return false
}
if (this.ws.readyState !== WebSocket.OPEN) {
logger.error(`${this.gwcid}Has been closed, on onMessage`)
return
Expand Down Expand Up @@ -44,6 +51,9 @@ class MessageEventEmitter extends EventEmitter {
}
// 处理请求
request (headers, body, start) {
if (!this.isConstructored) {
return Promise.reject(new Error('Has been destroyed'))
}
var requestId
requestId = headers.request_id = headers.request_id || workerUtil.createRequestId()
return ddvRowraw.stringifyPromise(headers, body, start)
Expand All @@ -59,6 +69,9 @@ class MessageEventEmitter extends EventEmitter {
}
// 收到请求结果-处理响应
onMessageResponse (res) {
if (!this.isConstructored) {
return false
}
var requestId, code, e, t
if (!(res.headers && (requestId = res.headers.request_id || res.headers.requestId || res.headers.requestid))) {
logger.error(res)
Expand All @@ -78,5 +91,16 @@ class MessageEventEmitter extends EventEmitter {
}
}
}
// 销毁
destroy () {
process.nextTick(() => {
var key
for (key in this) {
if (!this.hasOwnProperty(key)) continue
delete this[key]
}
key = void 0
})
}
}
module.exports = MessageEventEmitter
1 change: 1 addition & 0 deletions lib/serverRpcEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ class ServerRpcEvent {
var indexs
// 清理定时器
Array.isArray(this.saveGwcidTimers) && this.saveGwcidTimers.forEach(timer => clearTimeout(timer))
this.saveGwcidTimers = []
if (index !== void 0 && isSave !== true) {
// 加入保存gwcid 的 indexs 中
this.saveGwcidListsIndexs.indexOf(index) > -1 || this.saveGwcidListsIndexs.push(index)
Expand Down
30 changes: 17 additions & 13 deletions middleware/restfulPushServer/PushBaseEvent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'
const logger = require('../../lib/logger.js')
const MessageEventEmitter = require('../../lib/MessageEventEmitter.js')
const PushError = require('./PushError')
class PushBaseEvent extends MessageEventEmitter {
constructor (ws, req, options) {
super(req.gwcid)
Expand All @@ -21,33 +22,37 @@ class PushBaseEvent extends MessageEventEmitter {
// 初始化
wsEventBaseInit () {
this.ws.on('message', this.onMessage.bind(this))
this.ws.on('close', this.onClose.bind(this))
// 获取文件事件
this.on('protocol::push', this.onMessagePush.bind(this))
}
// 推送类型的信息
onMessagePush (res) {
if (!this.isConstructored) {
return false
}
if (!(res.method && res.path && this.emit(['push', res.method.toLowerCase(), res.path], res.headers, res.body, res))) {
logger.error(`[gwcid:${this.gwcid}]onMessagePush error`)
this.send(`Push request not found, not find method:${res.method}`)
.catch(e => {
logger.error(e)
})
}
}
// 收到消息的时候
onClose () {

}
// 关闭ws
close () {
if (!this.isConstructored) {
return Promise.reject(new PushError('Has been destroyed', 'HAS_BEEN_DESTROYED'))
}
return new Promise((resolve, reject) => {
this.ws.close.apply(this.ws, arguments)
resolve()
})
}
// 发送
send (data, options) {
if (!this.isConstructored) {
return Promise.reject(new PushError('Has been destroyed', 'HAS_BEEN_DESTROYED'))
}
return new Promise((resolve, reject) => {
return this.sendWs(data, options, e => {
e ? reject(e) : resolve()
Expand All @@ -56,23 +61,22 @@ class PushBaseEvent extends MessageEventEmitter {
}
// 发送ws
sendWs () {
if (!this.isConstructored) {
return false
}
return this.ws.send.apply(this.ws, arguments)
}
// 销毁
destroy () {
if (!this.isConstructored) {
return false
}
this.close()
.catch(e => {
logger.error(`[gwcid:${this.gwcid}] Failed to close at the time of destroy`)
})
.then(() => {
process.nextTick(() => {
var key
for (key in this) {
if (!this.hasOwnProperty(key)) continue
delete this[key]
}
key = void 0
})
super.destroy()
})
}
}
Expand Down
Loading

0 comments on commit 0ddc907

Please sign in to comment.