Guest User

Untitled

a guest
Jun 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. class TestHandler(tornado.websocket.WebSocketHandler):
  2. def open(self):
  3. print('open')
  4.  
  5. def on_message(self, message):
  6. print('received', message, self)
  7.  
  8.  
  9. class Application(tornado.web.Application):
  10. def __init__(self):
  11. handlers = [
  12. (r"/ws1/.*", TestHandler),
  13. ]
  14.  
  15. tornado.web.Application.__init__(self, handlers)
  16.  
  17.  
  18. ws_app = Application()
  19. ws_app.listen(8000)
  20. tornado.ioloop.IOLoop.instance().start()
  21.  
  22. from tornado.websocket import websocket_connect
  23. import asyncio
  24. import json
  25.  
  26. class Test:
  27.  
  28. async def hello(self):
  29. print('hello')
  30. test_connection = await websocket_connect('ws://localhost:8000/ws1/data_stream1')
  31. payload = json.dumps({'test': 'sending a message'})
  32. test_connection.write_message(payload)
  33.  
  34. while True:
  35. test_connection.write_message(payload)
  36. msg = await test_connection.read_message()
  37. if msg is None:
  38. print('no message!')
  39. break
  40. await asyncio.sleep(2)
  41.  
  42.  
  43. loop = asyncio.get_event_loop()
  44. test_class = Test()
  45. loop.run_until_complete(test_class.hello())
  46. loop.close()
  47.  
  48. from tornado.websocket import websocket_connect
  49. import asyncio
  50. import json
  51.  
  52. class Test:
  53.  
  54. async def hello(self):
  55. print('hello')
  56. test_connection = await websocket_connect('ws://localhost:8000/ws1/data_stream1')
  57. payload = json.dumps({'test': 'sending a message2'})
  58. test_connection.write_message(payload)
  59.  
  60. while True:
  61. test_connection.write_message(payload)
  62. msg = await test_connection.read_message()
  63. if msg is None:
  64. print('no message!')
  65. break
  66. await asyncio.sleep(1)
  67.  
  68.  
  69. loop = asyncio.get_event_loop()
  70. test_class = Test()
  71. loop.run_until_complete(test_class.hello())
  72. loop.close()
  73.  
  74. received {"test": "sending a message2"} <__main__.TestHandler object at 0x000001C4703E04A8>
  75. received {"test": "sending a message2"} <__main__.TestHandler object at 0x000001C4703E04A8>
  76. received {"test": "sending a message"} <__main__.TestHandler object at 0x000001C4703C5EF0>
  77. received {"test": "sending a message2"} <__main__.TestHandler object at 0x000001C4703E04A8>
  78. received {"test": "sending a message2"} <__main__.TestHandler object at 0x000001C4703E04A8>
  79. received {"test": "sending a message"} <__main__.TestHandler object at 0x000001C4703C5EF0>
  80. received {"test": "sending a message2"} <__main__.TestHandler object at 0x000001C4703E04A8>
  81. received {"test": "sending a message2"} <__main__.TestHandler object at 0x000001C4703E04A8>
  82. received {"test": "sending a message"} <__main__.TestHandler object at 0x000001C4703C5EF0>
Add Comment
Please, Sign In to add comment