Skip to content

Commit

Permalink
引入QQ频道websocket心跳简单单例模式
Browse files Browse the repository at this point in the history
  • Loading branch information
lunzhiPenxil committed Dec 7, 2021
1 parent 7df145d commit 9ade0d3
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions OlivOS/qqGuildLinkServerAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import websocket
import ssl
import asyncio
import uuid
import requests as req

import OlivOS
Expand All @@ -43,7 +44,8 @@ def __init__(self, Proc_name, scan_interval = 0.001, dead_interval = 1, rx_queue
'websocket_url': None,
'pulse_interval': None,
'last_s': None,
'ws_obj': None
'ws_obj': None,
'ws_item': None
}
self.Proc_data['platform_bot_info_dict'] = None

Expand Down Expand Up @@ -100,15 +102,14 @@ def on_error(self, ws, error):
self.log(0, 'OlivOS qqGuild link server [' + self.Proc_name + '] websocket link error')

def on_close(self, ws, close_status_code, close_msg):
self.Proc_data['extend_data']['pulse_interval'] = None
self.Proc_data['extend_data']['ws_obj'] = None
self.log(0, 'OlivOS qqGuild link server [' + self.Proc_name + '] websocket link close')

def on_open(self, ws):
self.log(2, 'OlivOS qqGuild link server [' + self.Proc_name + '] websocket link start')


def run_pulse(self):
tmp_ws_item = self.Proc_data['extend_data']['ws_item']
while self.Proc_data['extend_data']['pulse_interval'] != None:
tmp_pulse_interval = self.Proc_data['extend_data']['pulse_interval']
if tmp_pulse_interval > 1:
Expand All @@ -117,6 +118,9 @@ def run_pulse(self):
tmp_data = OlivOS.qqGuildSDK.PAYLOAD.sendHeartbeat(
self.Proc_data['extend_data']['last_s']
).dump()
if tmp_ws_item != self.Proc_data['extend_data']['ws_item'] or self.Proc_data['extend_data']['ws_item'] == None:
self.log(0, 'OlivOS qqGuild link server [' + self.Proc_name + '] websocket pulse giveup')
return
if self.Proc_data['extend_data']['ws_obj'] != None:
try:
self.Proc_data['extend_data']['ws_obj'].send(tmp_data)
Expand All @@ -138,7 +142,9 @@ def run_websocket_rx_connect_start(self):
on_close = self.on_close
)
self.Proc_data['extend_data']['ws_obj'] = ws
self.Proc_data['extend_data']['ws_item'] = uuid.uuid4()
ws.run_forever()
self.Proc_data['extend_data']['pulse_interval'] = None
self.Proc_data['extend_data']['ws_obj'] = None
self.Proc_data['extend_data']['ws_item'] = None
self.log(2, 'OlivOS qqGuild link server [' + self.Proc_name + '] websocket link lost')

0 comments on commit 9ade0d3

Please sign in to comment.