Permalink
Browse files

Clarify treatment of multi line data

  • Loading branch information...
boppreh committed Apr 16, 2014
1 parent ab1bdc5 commit adb853b8bf49440851895e9f587fd9ec39dc7471
Showing with 11 additions and 3 deletions.
  1. +4 −3 sse.py
  2. +7 −0 tests.py
View
7 sse.py
@@ -41,7 +41,8 @@ def _publish_single(self, data, queue):
"""
str_data = str(data)
for line in str_data.split('\n'):
queue.put(line)
queue.put('data: {}\n'.format(line))
queue.put('\n')
def publish(self, data, channel='default channel'):
"""
@@ -87,7 +88,7 @@ def subscribe(self, channel='default channel', properties=None, initial_data=[])
subscriber = (queue, properties)
for data in initial_data:
queue.put(str(data))
self._publish_single(data, queue)
for subscribers_list in self._get_subscribers_lists(channel):
subscribers_list.append(subscriber)
@@ -103,7 +104,7 @@ def _make_generator(self, queue):
data = queue.get()
if data is Publisher.END_STREAM:
return
yield 'data: {}\n\n'.format(data)
yield data
def close(self):
View
@@ -58,5 +58,12 @@ def test_initial_data(self):
'data: start 2\n\n'
'data: test')
def test_multiline(self):
p = Publisher()
s = p.subscribe()
p.publish('line 1\nline 2')
p.close()
self.assertEqual(self.read(s), 'data: line 1\ndata: line 2')
if __name__ == '__main__':
unittest.main()

0 comments on commit adb853b

Please sign in to comment.