Created
September 2, 2014 11:49
-
-
Save ir4y/8c7836bb1bbae9f1c9e2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # Есть структура данных описывающая пользователей | |
| # В socket будет попадать открытый обработчик WebSockerHandler | |
| user_list = [{'socket': tornado.websocket.WebSocketHandler(), | |
| 'groups': ['all', 'some group']}, | |
| {'socket': tornado.websocket.WebSocketHandler(), | |
| 'groups': ['other group']}] | |
| #Функция отправки сообщения пользователям | |
| def send_message(message): | |
| def _send_message(user): | |
| user['socket'].ws_connection.write_message(message) | |
| return _send_message | |
| #отправить сообщение всем пользователям | |
| def send_to_all(message): | |
| map(send_message(message), user_list) | |
| #Функция проверяющая принадлежность пользователя группе | |
| def has_group(group): | |
| def _has_group(user): | |
| return group in user['groups'] | |
| return _has_group | |
| #отправить сообщение всем пользователям из определенной группы | |
| def send_to_group(group, message) | |
| map(send_message(message), | |
| filter(has_group(group), | |
| user_list)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment