Here are the examples of the python api nats.protocol.Protocol.CR_LF taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
3 Examples
0
Example 1
def publish(self, subject, msg=Protocol.EMPTY_MSG, opt_reply="", blk=None):
'''\
Publish a message to a given subject, with optional reply subject and
completion block
Params:
=====
subject: message subject;
msg: message body;
opt_reply: reply inbox if needs;
blk: closure called when publish has been processed by the server.
'''
if not self.conn.connected:
raise NatsClientException("Connection losted")
if not subject:
return None
msg = str(msg)
self.stat.msgs_sent += 1
self.stat.bytes_sent += len(msg)
self.conn.send_command("PUB {} {} {}{}{}{}".format(subject,
opt_reply, len(msg), Protocol.CR_LF, msg, Protocol.CR_LF))
if blk:
self.ping_timer.queue_server_rt(blk)
0
Example 2
def subscribe(self, subject, callback=None, **opts):
'''\
Subscribe to a subject with optional wildcards.
Messages will be delivered to the supplied callback.
Callback can take any number of the supplied arguments as defined by the
list: msg, reply, sub.
Params:
=====
subject: optionally with wilcards.
opts: optional options hash, e.g. "queue", "max".
callback, called when a message is delivered.
Returns:
=====
sid: Subject Identifier
Returns subscription id which can be passed to #unsubscribe.
'''
if not self.conn.connected:
raise NatsClientException("Connection losted")
if not subject:
return None
sid = Common.get_ssid()
queue_str = ""
sub = {"subject" : subject, "received" : 0}
sub["callback"] = callback
if "queue" in opts:
queue_str = sub["queue"] = opts["queue"]
if "max" in opts:
sub["max"] = opts["max"]
self.conn.send_command("SUB {} {} {}{}".format(subject, queue_str,
sid, Protocol.CR_LF))
self.subs[sid] = sub
# Setup server support for auto-unsubscribe
if "max" in opts:
self.unsubscribe(sid, opts["max"])
return sid
0
Example 3
def unsubscribe(self, sid, opt_max=None):
'''\
Cancel a subscription.
Params:
=====
sid: Subject Identifier
opt_max: optional number of responses to receive
before auto-unsubscribing.
'''
if not self.conn.connected:
raise NatsClientException("Connection losted")
opt_max_str = ""
if opt_max:
opt_max_str = " " + str(opt_max)
self.conn.send_command("UNSUB {}{}{}".format(sid,
opt_max_str, Protocol.CR_LF))
if not sid in self.subs:
return None
sub = self.subs[sid]
sub["max"] = opt_max
if not (sub["max"] and (sub["received"] < sub["max"])):
del self.subs[sid]