class ThreadingHandler(threading.Thread):
host = ''
name = ''
q = ''
def __init__(self, host, name, queue):
'''
重写父类 init
:param host: 服务器地址
:param name: 服务器名称
'''
super().__init__()
self.host = host
self.name = name
self.q = queue
def run(self):
ws = WebSocketHandler(self.host, self.name)
# 这里往后就没在执行了
print(ws)
class WebSocketHandler():
"""
socket handler
"""
# websocket 服务地址
host = ''
# 服务器名称
name = ''
# websocket 实例
socketInstance = ''
# 服务链接状态
connectStatus = False
def __init__(self, host, name):
'''
:param host:服务器地址
:param name:服务器名称
:return
'''
self.host = host
self.name = name
# 连接服务器
self.connect()
return self.socketInstance
def connect(self):
'''
链接服务器
'''
# debug 模式
# websocket.enableTrace(True)
self.socketInstance = websocket.WebSocketApp(
self.host,
on_open = self.onOpen,
on_message = self.onMessage,
on_error = self.onError,
on_close = self.onClose
)
self.socketInstance.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})