-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathreconnect.py
More file actions
83 lines (57 loc) · 1.95 KB
/
Copy pathreconnect.py
File metadata and controls
83 lines (57 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Reconnect and resubscribe example.
This example keeps running until Ctrl+C. Stop and restart the broker to watch the
client reconnect and run setup again. With the public broker, it simply stays
connected and demonstrates where reconnect setup belongs.
Run with the public SimpleMQ broker:
python examples/reconnect.py
Run with a local broker:
python examples/reconnect.py http://localhost/smq.lsp
"""
from __future__ import annotations
import sys
import time
import uuid
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from smqclient import SMQClient
url = sys.argv[1] if len(sys.argv) > 1 else "https://simplemq.com/smq.lsp"
client_uid = "python-reconnect-" + uuid.uuid4().hex[:12]
def setup():
smq.subscribe("python.example.status", "update", {"onmsg": on_status})
smq.subscribe("self", "rpc.status", {"onmsg": on_status_rpc})
print("setup complete")
def on_status(data, ptid, tid, subtid):
print("status update:", data, "from", ptid)
def on_status_rpc(data, ptid, tid, subtid):
print("status RPC from", ptid)
smq.publish(b"ok", ptid, "rpc.status.reply")
def onconnect(etid, rnd, ipaddr):
print("connected", etid)
setup()
def onreconnect(etid, rnd, ipaddr):
print("reconnected", etid)
setup()
def onclose(reason, can_reconnect):
print("closed:", reason, "can_reconnect:", can_reconnect)
return 2 if can_reconnect else False
smq = SMQClient.create(
url,
{
"uid": client_uid,
"info": "Python reconnect example",
"onconnect": onconnect,
"onreconnect": onreconnect,
"onclose": onclose,
"reconnect": True,
"reconnect_delay": 2,
},
)
try:
print("connecting to", url)
print("press Ctrl+C to stop")
while True:
time.sleep(1)
except KeyboardInterrupt:
print("stopping")
finally:
smq.disconnect()