gpt4 book ai didi

python - 如何将从websocket接收到的on_message数据返回到类外?使用 python websocket 客户端

转载 作者:行者123 更新时间:2023-12-04 17:38:40 29 4
gpt4 key购买 nike

我试图让我的代码从 Websocket 连接获取一些数据,然后使用来自 WS 响应的数据。问题是,我不知道如何让代码等待 WS 应答,然后将数据“发送”到 WS 类之外。

我正在使用此 websocket-client 库 ( https://github.com/websocket-client/websocket-client ) 并使用该页面中的“长期连接”示例。

import websocket
import _thread as thread
import time, json

class Frame:
def __init__(self):
self.m = 0
self.i = 0
self.n = ""
self.o = ""

class MyWS():
def __init__(self):
self.wsadd = 'ws://websocket.org/' #Example
self.frame = Frame()

self.openWS()

def on_message(self, message):
msg = json.loads(message)
print(msg)
if msg["n"] == 'SubscribeData':
self.sub = json.loads(msg['o'])
return self.sub #It doesn't seem to do anything

def on_error(self, error):
print(error)

def on_close(self):
print("WS closed")

def on_open(self):
def run(*args):
print('WS Open')
#self.Authenticate() #Commented because haven't code it yet
#self.sendWS()
thread.start_new_thread(run, ())

def openWS(self):
websocket.enableTrace(True)
self.ws = websocket.WebSocketApp(self.wsadd, on_message = self.on_message, on_open = self.on_open, on_error = self.on_error, on_close = self.on_close)
self.wst = threading.Thread(target=lambda: self.ws.run_forever())
self.wst.daemon = True
self.wst.start()

def sendWS(self):
self.ws.send(json.dumps(self.frame.__dict__))


def SubscribeData(self):
self.frame.m = 0
self.frame.i = int(time.time())
self.frame.n = 'SubscribeData'
payload = {
"OMSId": 1,
"InstrumentId": 1,
}
self.frame.o = json.dumps(payload)
self.sendWS(self.frame)
#return #Should return something here?

obj = MyWS() #When the obj is instantiated the WS Connection is opened.

result = obj.SubscribeData() #This sends a request for subscribing to the datafeed. It get's an answer but isn't saved in the variable. (because the method doesn't really return anything)

print(result) #prints None

当我实例化 MyWS 类时,WS 连接是开放式的。

当我使用 SubscibeData 方法时,我得到了预期的响应。 所以 websocket 部分工作正常。

on_message 方法将响应保存在 self.sub 中,但不会在任何地方返回。

我真正需要的是一种将接收到的数据发送到类“外部”的方法,这样“外部代码”不仅可以等待接收数据,还可以对其进行操作

我是 websockets 的新手,所以你知道......

最佳答案

为了回答@HJA24 以及其他可能偶然发现这个问题的人,我并没有真正找到一个(好的)解决方案,但做了一个解决方法。

由于 on_message 方法将返回值保存在 self.sub 中,我创建了另一个返回 self.sub 值的方法,这样我可以访问 WS 响应。如果您决定使用此方法,则必须考虑到 WS 可能需要比您调用返回值的方法所需的时间更长的时间来回答您。

像这样:

import websocket
import ...

class MyWS():
def __init__(self):
self.wsadd = 'ws://websocket.org/' #Example
self.frame = Frame()
self.openWS()

def on_message(self, message):
msg = json.loads(message)
if msg["n"] == 'SubscribeData':
self.sub = json.loads(msg['o'])

def get_subscribe_data(self):
return self.sub

def on_error(self, error):
...

obj = MyWS() #When the obj is instantiated the WS Connection is opened.

obj.SubscribeData()
result = obj.get_subscribe_data()
print(result) #prints the data on self.sub

我不接受我自己的回答,因为肯定有更好的方法来做到这一点,所以如果您正在阅读本文并有想法,请随时分享。

关于python - 如何将从websocket接收到的on_message数据返回到类外?使用 python websocket 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55562681/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com