缘起
因为需要将MQTT中的数据推送给前端,但是前端不会直接连接MQTT,所以服务端做了一个中间层,通过websocket推送,但是在开发的过程中前端总是认为推送的数据不及时,所以这里又实用Python单独做了一个客户端做时间记录验证。开始吧
安装三方包
Python的生态很方便,这里直接使用 websocket-client
安装
pip install websocket-client -i https://pypi.tuna.tsinghua.edu.cn/simple
这里使用了清华的源,方便,也比较快,如果不使用清华的貌似需要蛮久的
非常快的安装完成
在控制台修改为永久使用清华源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
实现客户端代码
import websocket
def on_message(ws, message):
print(f"Received from server: {