-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsub_only.py
executable file
·56 lines (43 loc) · 1.62 KB
/
sub_only.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from sys import stdout
import txnats
from twisted.logger import globalLogPublisher
from simple_log_observer import simpleObserver
from twisted.logger import Logger
log = Logger()
from twisted.internet import reactor
from twisted.internet.endpoints import TCP4ClientEndpoint
from twisted.internet.endpoints import connectProtocol
def on_happy_msg(nats_protocol, sid, subject, reply_to, payload):
stdout.write(
"sid: {}, subject: {}, reply-to: {}\r\n".format(
sid, subject, reply_to))
stdout.write(payload.decode())
stdout.write("\r\n*")
def create_client(reactor, host, port):
"""
Start a Nats Protocol and connect it to a NATS endpoint over TCP4
which subscribes to a subject.
"""
log.info("Start client.")
point = TCP4ClientEndpoint(reactor, host, port)
nats_protocol = txnats.io.NatsProtocol(
verbose=False,
on_connect=lambda np: np.sub("happy", "6", on_msg=on_happy_msg))
# Because NatsProtocol implements the Protocol interface, Twisted's
# connectProtocol knows how to connected to the endpoint.
connecting = connectProtocol(point, nats_protocol)
# Log if there is an error making the connection.
connecting.addErrback(lambda np: log.info("{p}", p=np))
# Log what is returned by the connectProtocol.
connecting.addCallback(lambda np: log.info("{p}", p=np))
return connecting
def main(reactor):
host = "demo.nats.io"
port = 4222
create_client(reactor, host, port)
if __name__ == '__main__':
globalLogPublisher.addObserver(simpleObserver)
main(reactor)
reactor.run()