baseimage-tutorial-framework/lib/tfw/networking/message_sender.py
2018-06-04 22:16:44 +02:00

33 lines
1011 B
Python

# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details.
from datetime import datetime
from tfw.networking.event_handlers import ServerUplinkConnector
class MessageSender:
"""
Provides a mechanism to send messages to our frontend messaging component which
displays messages with the key "message".
"""
def __init__(self, custom_key: str = None):
self.server_connector = ServerUplinkConnector()
self.key = custom_key or 'message'
def send(self, originator, message):
"""
Sends a message to the key specified in __init__.
:param originator: name of sender to be displayed on the frontend
:param message: message to send
"""
data = {
'originator': originator,
'timestamp': datetime.now().isoformat(),
'message': message
}
self.server_connector.send({
'key': self.key,
'data': data
})