Skip to content

Commit 3b32dbd

Browse files
expose the sid for the connection
1 parent 9847565 commit 3b32dbd

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

docs/client.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ In the case of the ``asyncio`` client, the method is a coroutine::
9292

9393
await sio.connect('http://localhost:5000')
9494

95+
Upon connection, the server assigns the client a unique session identifier.
96+
The applicaction can find this identifier in the ``sid`` attribute::
97+
98+
print('my sid is', sio.sid)
99+
95100
Emitting Events
96101
---------------
97102

socketio/asyncio_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,10 @@ async def _handle_reconnect(self):
386386
'Maximum reconnection attempts reached, giving up')
387387
break
388388

389-
def _handle_eio_connect(self): # pragma: no cover
389+
def _handle_eio_connect(self):
390390
"""Handle the Engine.IO connection event."""
391391
self.logger.info('Engine.IO connection established')
392+
self.sid = self.eio.sid
392393

393394
async def _handle_eio_message(self, data):
394395
"""Dispatch Engine.IO messages."""
@@ -426,6 +427,7 @@ async def _handle_eio_disconnect(self):
426427
await self._trigger_event('disconnect', namespace='/')
427428
self.callbacks = {}
428429
self._binary_packet = None
430+
self.sid = None
429431
if self.eio.state == 'connected' and self.reconnection:
430432
self._reconnect_task = self.start_background_task(
431433
self._handle_reconnect)

socketio/client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def __init__(self, reconnection=True, reconnection_attempts=0,
9494
self.connection_transports = None
9595
self.connection_namespaces = None
9696
self.socketio_path = None
97+
self.sid = None
9798

9899
self.namespaces = []
99100
self.handlers = {}
@@ -481,9 +482,10 @@ def _handle_reconnect(self):
481482
'Maximum reconnection attempts reached, giving up')
482483
break
483484

484-
def _handle_eio_connect(self): # pragma: no cover
485+
def _handle_eio_connect(self):
485486
"""Handle the Engine.IO connection event."""
486487
self.logger.info('Engine.IO connection established')
488+
self.sid = self.eio.sid
487489

488490
def _handle_eio_message(self, data):
489491
"""Dispatch Engine.IO messages."""
@@ -521,6 +523,7 @@ def _handle_eio_disconnect(self):
521523
self._trigger_event('disconnect', namespace='/')
522524
self.callbacks = {}
523525
self._binary_packet = None
526+
self.sid = None
524527
if self.eio.state == 'connected' and self.reconnection:
525528
self._reconnect_task = self.start_background_task(
526529
self._handle_reconnect)

tests/asyncio/test_asyncio_client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,13 @@ def test_handle_reconnect_max_attempts(self, random):
589589
])
590590
self.assertEqual(c._reconnect_task, 'foo')
591591

592+
def test_eio_connect(self):
593+
c = asyncio_client.AsyncClient()
594+
c.eio.sid = 'foo'
595+
self.assertIsNone(c.sid)
596+
c._handle_eio_connect()
597+
self.assertEqual(c.sid, 'foo')
598+
592599
def test_handle_eio_message(self):
593600
c = asyncio_client.AsyncClient()
594601
c._handle_connect = AsyncMock()
@@ -630,20 +637,24 @@ def test_handle_eio_message(self):
630637
def test_eio_disconnect(self):
631638
c = asyncio_client.AsyncClient()
632639
c._trigger_event = AsyncMock()
640+
c.sid = 'foo'
633641
c.eio.state = 'connected'
634642
_run(c._handle_eio_disconnect())
635643
c._trigger_event.mock.assert_called_once_with(
636644
'disconnect', namespace='/')
645+
self.assertIsNone(c.sid)
637646

638647
def test_eio_disconnect_namespaces(self):
639648
c = asyncio_client.AsyncClient()
640649
c.namespaces = ['/foo', '/bar']
641650
c._trigger_event = AsyncMock()
651+
c.sid = 'foo'
642652
c.eio.state = 'connected'
643653
_run(c._handle_eio_disconnect())
644654
c._trigger_event.mock.assert_any_call('disconnect', namespace='/foo')
645655
c._trigger_event.mock.assert_any_call('disconnect', namespace='/bar')
646656
c._trigger_event.mock.assert_any_call('disconnect', namespace='/')
657+
self.assertIsNone(c.sid)
647658

648659
def test_eio_disconnect_reconnect(self):
649660
c = asyncio_client.AsyncClient(reconnection=True)

tests/common/test_client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def test_create(self, engineio_client_class):
4343
self.assertEqual(c.connection_transports, None)
4444
self.assertEqual(c.connection_namespaces, None)
4545
self.assertEqual(c.socketio_path, None)
46+
self.assertEqual(c.sid, None)
4647

4748
self.assertEqual(c.namespaces, [])
4849
self.assertEqual(c.handlers, {})
@@ -689,6 +690,13 @@ def test_handle_reconnect_max_attempts(self, random):
689690
])
690691
self.assertEqual(c._reconnect_task, 'foo')
691692

693+
def test_handle_eio_connect(self):
694+
c = client.Client()
695+
c.eio.sid = 'foo'
696+
self.assertIsNone(c.sid)
697+
c._handle_eio_connect()
698+
self.assertEqual(c.sid, 'foo')
699+
692700
def test_handle_eio_message(self):
693701
c = client.Client()
694702
c._handle_connect = mock.MagicMock()
@@ -730,20 +738,24 @@ def test_eio_disconnect(self):
730738
c = client.Client()
731739
c._trigger_event = mock.MagicMock()
732740
c.start_background_task = mock.MagicMock()
741+
c.sid = 'foo'
733742
c.eio.state = 'connected'
734743
c._handle_eio_disconnect()
735744
c._trigger_event.assert_called_once_with('disconnect', namespace='/')
745+
self.assertIsNone(c.sid)
736746

737747
def test_eio_disconnect_namespaces(self):
738748
c = client.Client()
739749
c.namespaces = ['/foo', '/bar']
740750
c._trigger_event = mock.MagicMock()
741751
c.start_background_task = mock.MagicMock()
752+
c.sid = 'foo'
742753
c.eio.state = 'connected'
743754
c._handle_eio_disconnect()
744755
c._trigger_event.assert_any_call('disconnect', namespace='/foo')
745756
c._trigger_event.assert_any_call('disconnect', namespace='/bar')
746757
c._trigger_event.assert_any_call('disconnect', namespace='/')
758+
self.assertIsNone(c.sid)
747759

748760
def test_eio_disconnect_reconnect(self):
749761
c = client.Client(reconnection=True)

0 commit comments

Comments
 (0)