Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TestHandler(tornado.websocket.WebSocketHandler):
- def open(self):
- print('open')
- def on_message(self, message):
- print('received', message, self)
- class Application(tornado.web.Application):
- def __init__(self):
- handlers = [
- (r"/ws1/.*", TestHandler),
- ]
- tornado.web.Application.__init__(self, handlers)
- ws_app = Application()
- ws_app.listen(8000)
- tornado.ioloop.IOLoop.instance().start()
- from tornado.websocket import websocket_connect
- import asyncio
- import json
- class Test:
- async def hello(self):
- print('hello')
- test_connection = await websocket_connect('ws://localhost:8000/ws1/data_stream1')
- payload = json.dumps({'test': 'sending a message'})
- test_connection.write_message(payload)
- while True:
- test_connection.write_message(payload)
- msg = await test_connection.read_message()
- if msg is None:
- print('no message!')
- break
- await asyncio.sleep(2)
- loop = asyncio.get_event_loop()
- test_class = Test()
- loop.run_until_complete(test_class.hello())
- loop.close()
- from tornado.websocket import websocket_connect
- import asyncio
- import json
- class Test:
- async def hello(self):
- print('hello')
- test_connection = await websocket_connect('ws://localhost:8000/ws1/data_stream1')
- payload = json.dumps({'test': 'sending a message2'})
- test_connection.write_message(payload)
- while True:
- test_connection.write_message(payload)
- msg = await test_connection.read_message()
- if msg is None:
- print('no message!')
- break
- await asyncio.sleep(1)
- loop = asyncio.get_event_loop()
- test_class = Test()
- loop.run_until_complete(test_class.hello())
- loop.close()
- received {"test": "sending a message2"} <__main__.TestHandler object at 0x000001C4703E04A8>
- received {"test": "sending a message2"} <__main__.TestHandler object at 0x000001C4703E04A8>
- received {"test": "sending a message"} <__main__.TestHandler object at 0x000001C4703C5EF0>
- received {"test": "sending a message2"} <__main__.TestHandler object at 0x000001C4703E04A8>
- received {"test": "sending a message2"} <__main__.TestHandler object at 0x000001C4703E04A8>
- received {"test": "sending a message"} <__main__.TestHandler object at 0x000001C4703C5EF0>
- received {"test": "sending a message2"} <__main__.TestHandler object at 0x000001C4703E04A8>
- received {"test": "sending a message2"} <__main__.TestHandler object at 0x000001C4703E04A8>
- received {"test": "sending a message"} <__main__.TestHandler object at 0x000001C4703C5EF0>
Add Comment
Please, Sign In to add comment