- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图让我的代码从 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/
我正在使用discord.py 编写一个discord 机器人。我编写了一个初始版本,但我认为所有这些都需要重新组织,因此我将代码移动到不同的文件中。 代码与以前相同,但现在当我启动机器人时,机器人检
在 gui 应用程序中有 ON_MESSAGE(id, handler) 和 hanler 的 LRESULT CMainFrame::handler(WPARAM wParam, LPARAM lP
如何接收 MQTT 并将其传递给我设置的 bash 脚本?我收到的值在我发送的消息和收到消息之间不断增加,我无法判断它是否已执行 bash 脚本,并且它不会打印后面的行进行检查,所以我怀疑有什么问题之
我的代码运行正常。只有一个问题,即在调用任何命令后,我的 on_message 会立即被调用(这会导致一些副作用) async def delete_on_swear(message): if
当我将太多参数传递给f()时,为什么python不抛出错误或异常?显然,由于该错误,未执行f()。我正在使用VS-Code和python 3.8。 (如果要复制,则必须安装websocket_clie
我使用python从nodejs服务器接收流数据,使用以下python代码。websocket 应该获取实时流数据。我假设 python 的 WebSocketApp 获取流数据的唯一方法是通过 on
为什么我不能有多个 on_message 事件? import discord client = discord.Client() @client.event async def on_ready()
基本上,一切似乎都可以正常工作并启动,但由于某种原因我无法调用任何命令。我现在已经轻松地环顾四周并查看示例/观看视频,但我终生无法找出问题所在。代码如下: import discord import
我正在尝试使机器人基本上处于 sleep 模式的命令,这意味着让机器人停止响应命令(如果可能的话,或 on_messages)尝试使用 client.pause (Boolean) 但它没有给出错误我
我有以下类(class): class SessionHandler(tornado.websocket.WebSocketHandler): @tornado.gen.coroutine
假设我已经订阅了某个主题,并且 mosquitto 服务器正在持续发布有关该主题的一些消息。 当新消息从 mosquitto 代理到达时,如何调用 on_message 方法?它是在主线程上运行,从而
我是 Tornado 的新手。我正在尝试用 Tornado 构建一个聊天服务器代理,我从网络客户端收到消息,通常它只需要将其发送回来,但是,我需要将这些消息发送给另一个服务器优先,问题来了,等待其他服
我最近开始用 Python 制作一个 Discord 机器人(用它测试 Python 的基础),并自己创建了一个带有多个命令的功能机器人。为了扩大其用途,我添加了一个级别/XP 系统,到目前为止该系统
当我的代码中有 on_message() 时,它会停止所有其他 @bot.command 命令的工作。我尝试await bot.process_commands(message),但这也不起作用。这是
我已经安装了 Mosquitto 并在终端中使用没有问题,还为 c++ 安装了 paho,mosquitto 和 paho 都运行良好。然后通过 pip 安装 paho for python。现在我正
请帮忙:)我刚开始在 python 中使用 MQTT。当我运行以下程序时: import paho.mqtt.client as mqtt def on_connect(client, userdat
我的 discord.py 脚本给了我一个意外错误。它基本上标记了我在 async def on_message(): 之外定义的所有变量。但在此之下用作“未定义”。 好吧,您可以通过在 on_mes
这让我发疯。我正在使用 Tornado 3.2 和 python 2.7。我已经在我的测试机 (Windows) 和我的 Linux 机器上都试过了。我只是在用(我认为是)一个简单的聊天室应用程序试水
我正在用 python 编写一个 discord-bot,它可以在 IntelliJ 甚至终端上正常运行。 当我尝试让它在 linux 服务器上运行但未连接到它时,问题就开始了 # Calle
最近我的 bot 越来越大,我花时间重写代码以使其与 Discord Py 的 cogs 系统一起工作 我已经正确调整了所有代码,但是我停止工作的所有 on_message 事件都没有抛出任何类型的错
我是一名优秀的程序员,十分优秀!