减少判断心跳超时的开销

This commit is contained in:
John Smith 2023-08-26 16:42:01 +08:00
parent 00c47a3e55
commit 60b9f81fe2
3 changed files with 6 additions and 8 deletions

View File

@ -165,15 +165,13 @@ class ChatHandler(tornado.websocket.WebSocketHandler): # noqa
def on_message(self, message):
try:
# 超时没有加入房间也断开
if self.has_joined_room:
self._refresh_receive_timeout_timer()
body = json.loads(message)
cmd = body['cmd']
if cmd == Command.HEARTBEAT:
pass
# 超时没有加入房间也断开
if self.has_joined_room:
self._refresh_receive_timeout_timer()
elif cmd == Command.JOIN_ROOM:
if self.has_joined_room:

View File

@ -183,7 +183,6 @@ export default class ChatClientDirect {
}
onWsMessage(event) {
this.refreshReceiveTimeoutTimer()
if (!(event.data instanceof ArrayBuffer)) {
console.warn('未知的websocket消息类型data=', event.data)
return
@ -226,6 +225,7 @@ export default class ChatClientDirect {
}
case OP_HEARTBEAT_REPLY: {
// 服务器心跳包,包含人气值,这里没用
this.refreshReceiveTimeoutTimer()
break
}
default: {

View File

@ -99,11 +99,11 @@ export default class ChatClientRelay {
}
onWsMessage(event) {
this.refreshReceiveTimeoutTimer()
let { cmd, data } = JSON.parse(event.data)
switch (cmd) {
case COMMAND_HEARTBEAT: {
this.refreshReceiveTimeoutTimer()
// 不能由定时器触发发心跳包因为浏览器会把不活动页面的定时器调到1分钟以上
this.websocket.send(JSON.stringify({
cmd: COMMAND_HEARTBEAT