Last active
January 4, 2016 02:39
-
-
Save aluminiumgeek/8556607 to your computer and use it in GitHub Desktop.
Tornado Websocket example for blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UpdatesHandler(websocket.WebSocketHandler): | |
socket_clients = set() | |
def open(self): | |
self.socket_clients.add(self) | |
def on_close(self): | |
self.socket_clients.remove(self) | |
@classmethod | |
def send_updates(cls, data): | |
for client in cls.socket_clients: | |
client.write_message(data) | |
# Использовать можно из любого места | |
data = { | |
'type': 'new_message', | |
'data': 'Hello!' | |
} | |
UpdatesHandler.send_updates(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment