Skip to content

Commit

Permalink
fix: handle graceful peer reconnect on network disruptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ivelin committed Mar 24, 2020
1 parent 20f12e2 commit 95ce08c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
26 changes: 13 additions & 13 deletions src/peerjs/ext/http-proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

LOG_LEVEL = logging.INFO

peer = None
savedPeerId = None
# persisted config dict
config = {}
Expand Down Expand Up @@ -235,11 +234,6 @@ async def pc_close():

async def pnp_service_connect() -> Peer:
"""Create a Peer instance and register with PnP signaling server."""
# if connection to pnp service already open, then nothing to do
global peer
if peer and peer.open:
log.info('peer already connected')
return
# Create own peer object with connection to shared PeerJS server
log.info('creating peer')
# If we already have an assigned peerId, we will reuse it forever.
Expand All @@ -262,9 +256,7 @@ async def pnp_service_connect() -> Peer:
await peer.start()
log.info('peer activated')
_setPnPServiceConnectionHandlers(peer)
log.info('Calling make_discoverable')
await make_discoverable(peer=peer)
log.info('Exited make_discoverable')
return peer


async def make_discoverable(peer=None):
Expand All @@ -282,9 +274,11 @@ async def make_discoverable(peer=None):
if peer.open:
await join_peer_room(peer=peer)
else:
log.info('Peer not connected to signaling server. '
'Will retry in a bit.')
if peer.disconnected:
log.info('Peer connected is not open.')
elif peer.destroyed:
log.info('Peer connection destroyed. Will create a new peer.')
peer = await pnp_service_connect()
elif peer.disconnected:
log.info('Peer disconnected. Will try to reconnect.')
await peer.reconnect()
else:
Expand Down Expand Up @@ -315,7 +309,13 @@ def _config_logger():
async def _start():
global http_session
http_session = aiohttp.ClientSession()
await pnp_service_connect()
peer = await pnp_service_connect()
if peer:
log.info('Calling make_discoverable')
await make_discoverable(peer=peer)
log.info('Exited make_discoverable')
else:
log.warning('No new peer created. An active peer already exists.')


async def _shutdown():
Expand Down
4 changes: 2 additions & 2 deletions src/peerjs/peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ def http_api(self):
# }

@property
def destroyed(self, ):
def destroyed(self):
"""Return True if peer connections have been destroyed."""
return self._destroyed

@property
def disconnected(self, ):
def disconnected(self):
"""Return True if peer is disconnected from signaling server."""
return self._disconnected

Expand Down

0 comments on commit 95ce08c

Please sign in to comment.