import os import tornado.ioloop from tornado.options import define, options, parse_command_line import tornado.web import socketio define("port", default=5000, help="run on the given port", type=int) define("debug", default=False, help="run in debug mode") sio = socketio.AsyncServer(async_mode='tornado') async def background_task(): """Example of how to send server generated events to clients.""" count = 0 while True: await sio.sleep(10) count += 1 await sio.emit('my_response', {'data': 'Server generated event'}) class MainHandler(tornado.web.RequestHandler): def get(self): self.render("app.html") @sio.event async def my_event(sid, message): await sio.emit('my_response', {'data': message['data']}, room=sid) @sio.event async def my_broadcast_event(sid, message): await sio.emit('my_response', {'data': message['data']}) @sio.event async def join(sid, message): await sio.enter_room(sid, message['room']) await sio.emit('my_response', {'data': 'Entered room: ' + message['room']}, room=sid) @sio.event async def leave(sid, message): await sio.leave_room(sid, message['room']) await sio.emit('my_response', {'data': 'Left room: ' + message['room']}, room=sid) @sio.event async def close_room(sid, message): await sio.emit('my_response', {'data': 'Room ' + message['room'] + ' is closing.'}, room=message['room']) await sio.close_room(message['room']) @sio.event async def my_room_event(sid, message): await sio.emit('my_response', {'data': message['data']}, room=message['room']) @sio.event async def disconnect_request(sid): await sio.disconnect(sid) @sio.event async def connect(sid, environ): await sio.emit('my_response', {'data': 'Connected', 'count': 0}, room=sid) @sio.event def disconnect(sid, reason): print('Client disconnected, reason:', reason) def main(): parse_command_line() app = tornado.web.Application( [ (r"/", MainHandler), (r"/socket.io/", socketio.get_tornado_handler(sio)), ], template_path=os.path.join(os.path.dirname(__file__), "templates"), static_path=os.path.join(os.path.dirname(__file__), "static"), debug=options.debug, ) app.listen(options.port) tornado.ioloop.IOLoop.current().start() if __name__ == "__main__": main()