对真实 nixmsg 跑 DEVELOPMENT 第 9 节接入清单(跳过仅 JS 跨域),补 README/示例,并修 Paho/HiveMQ 真机联调死锁与鉴权分类。
32 lines
917 B
Python
32 lines
917 B
Python
"""最小示例:连接、收发、关闭。
|
|
|
|
用法(需本地已启动 nixmsg,并开放注册或已有端):
|
|
python examples/minimal.py ws://127.0.0.1:PORT/mqtt device-1 password12 peer-id
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import time
|
|
|
|
from nixmsg import Body, Client, SendOptions, Target
|
|
|
|
|
|
def main() -> None:
|
|
if len(sys.argv) < 5:
|
|
print(__doc__)
|
|
raise SystemExit(2)
|
|
url, eid, password, peer = sys.argv[1:5]
|
|
c = Client()
|
|
c.on_session(lambda token: print("session", token[:16] + "..."))
|
|
c.on_message(lambda msg: print("msg", msg.from_id, msg.id, msg.body.data))
|
|
c.on_connection(lambda ev: print("conn", ev.state.value, ev.reason))
|
|
c.connect(url, eid, password=password)
|
|
c.send(Target(kind="endpoint", id=peer), Body(data="hello from python"), SendOptions(delay_ms=0))
|
|
time.sleep(2)
|
|
c.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|