mirror of
https://github.com/EQEmu/Server.git
synced 2025-12-22 02:51:30 +00:00
Added python example
This commit is contained in:
parent
22aef8ad79
commit
3bc9e4bc24
7
utils/nats/helloworld/README.md
Normal file
7
utils/nats/helloworld/README.md
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
Hello World NATS example
|
||||||
|
|
||||||
|
# helloworld2.py
|
||||||
|
* run `pip install nats-client`
|
||||||
|
* run `pip install protobuf`
|
||||||
|
|
||||||
|
|
||||||
47
utils/nats/helloworld/helloworld2.py
Normal file
47
utils/nats/helloworld/helloworld2.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
# coding: utf-8
|
||||||
|
import tornado.ioloop
|
||||||
|
import tornado.gen
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
sys.path.insert(0, "../../../protobuf/python/proto")
|
||||||
|
from datetime import datetime
|
||||||
|
from nats.io.utils import new_inbox
|
||||||
|
from nats.io import Client as NATS
|
||||||
|
import message_pb2
|
||||||
|
|
||||||
|
@tornado.gen.coroutine
|
||||||
|
def main():
|
||||||
|
nc = NATS()
|
||||||
|
|
||||||
|
# Establish connection to the server.
|
||||||
|
options = { "verbose": True, "servers": ["nats://127.0.0.1:4222"] }
|
||||||
|
yield nc.connect(**options)
|
||||||
|
|
||||||
|
def discover(msg=None):
|
||||||
|
channel_message = message_pb2.ChannelMessage()
|
||||||
|
channel_message.ParseFromString(msg.data)
|
||||||
|
print("[Received]: %s" % channel_message)
|
||||||
|
|
||||||
|
|
||||||
|
send_message = message_pb2.ChannelMessage()
|
||||||
|
send_message.chan_num = 5
|
||||||
|
#send_message.from = "python"
|
||||||
|
send_message.message = "Hello, World!"
|
||||||
|
|
||||||
|
sid = yield nc.subscribe("world.channel_message", "", discover)
|
||||||
|
|
||||||
|
yield nc.publish("world.channel_message", send_message.SerializeToString())
|
||||||
|
|
||||||
|
loop = tornado.ioloop.IOLoop.instance()
|
||||||
|
yield tornado.gen.Task(loop.add_timeout, time.time() + 20)
|
||||||
|
try:
|
||||||
|
start = datetime.now()
|
||||||
|
# Make roundtrip to the server and timeout after 1 second
|
||||||
|
yield nc.flush(1)
|
||||||
|
end = datetime.now()
|
||||||
|
print("Latency: %d µs" % (end.microsecond - start.microsecond))
|
||||||
|
except tornado.gen.TimeoutError, e:
|
||||||
|
print("Timeout! Roundtrip too slow...")
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
tornado.ioloop.IOLoop.instance().run_sync(main)
|
||||||
Loading…
x
Reference in New Issue
Block a user