2021-05-16 20:48:34 +00:00
|
|
|
import queue
|
|
|
|
|
import sse
|
|
|
|
|
from exceptions import TooManyConnections
|
|
|
|
|
|
|
|
|
|
class SseStream():
|
|
|
|
|
__queues = {}
|
|
|
|
|
__maxConnections = 10
|
|
|
|
|
__queue_size = 50
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def setMaxConnections(maxConnections = 10):
|
|
|
|
|
""" Set maximum number of concurrent connections
|
|
|
|
|
---
|
|
|
|
|
Paremeters:
|
|
|
|
|
------
|
|
|
|
|
maxConnections: int, optional
|
|
|
|
|
The number of maximum concurrent connections ( default value is 10) """
|
|
|
|
|
SseStream.__maxConnections = maxConnections
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def setQueueSize(queueSize = 50):
|
|
|
|
|
""" Set the queue size for stored messages
|
|
|
|
|
---
|
|
|
|
|
Parementers
|
|
|
|
|
------
|
|
|
|
|
queueSize: int, optional
|
|
|
|
|
The number of stored messages in the queue, set to <=0 for unlimited size ( default value is 50 ) """
|
|
|
|
|
SseStream.__queue_size = queueSize
|
|
|
|
|
|
|
|
|
|
def __init__(self, id):
|
|
|
|
|
""" Init a new stream
|
|
|
|
|
---
|
|
|
|
|
Paremeters:
|
|
|
|
|
------
|
|
|
|
|
id: string
|
|
|
|
|
The unique id of the client
|
|
|
|
|
"""
|
|
|
|
|
if len( self.__queues ) >= self.__maxConnections:
|
|
|
|
|
raise TooManyConnections
|
|
|
|
|
|
|
|
|
|
if not self.__queues.get( id ):
|
|
|
|
|
self._queue = queue.Queue( self.__queue_size )
|
|
|
|
|
self.__queues[ id ] = self._queue
|
|
|
|
|
|
|
|
|
|
print(f"Queue size: {self.__queue_size}; Connections: { len( self.__queues ) }/{self.__maxConnections}; ID: {id}")
|
|
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
|
""" Deregister client from queue - clean up """
|
|
|
|
|
print( "Stream object is deleted")
|
|
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
def __next__(self):
|
|
|
|
|
""" Method which is called regular to send out the event """
|
|
|
|
|
while True:
|
|
|
|
|
msg = self.queue.get(block=True, timeout=None)
|
2021-05-16 21:17:07 +00:00
|
|
|
if isinstance( msg, sse.Sse ):
|
2021-05-16 20:48:34 +00:00
|
|
|
for data in msg:
|
|
|
|
|
yield data
|
|
|
|
|
|
2021-05-16 21:17:07 +00:00
|
|
|
def addMessage(self, event_name, data, id = 0):
|
|
|
|
|
"""
|
|
|
|
|
Adds a event to the queue so it can be sent out
|
|
|
|
|
---
|
|
|
|
|
Parameters:
|
|
|
|
|
------
|
|
|
|
|
event_name: str
|
|
|
|
|
Name of the event
|
|
|
|
|
data: string or list of strings
|
|
|
|
|
Datat to be send
|
|
|
|
|
id: int
|
|
|
|
|
id of the event, if set to 0 no ID will be sent """
|
|
|
|
|
event = sse.Sse()
|
|
|
|
|
if id > 0:
|
|
|
|
|
event.set_event_id(id)
|
|
|
|
|
else:
|
|
|
|
|
event.reset_event_id()
|
|
|
|
|
event.add_message( event_name, data )
|
|
|
|
|
|
|
|
|
|
self._queue.put(event)
|
|
|
|
|
|
2021-05-16 20:48:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
SseStream.setMaxConnections(5)
|
|
|
|
|
SseStream.setQueueSize(10)
|
|
|
|
|
sse = SseStream("Test")
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|