Skip to content

Commit 1849be6

Browse files
committed
fix: polishing signaling server communication and room membership api
1 parent 62ca5d3 commit 1849be6

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

cli.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
peer = None
1919
myPeerId = None
20-
AMBIANIC_PNP_HOST = 'ambianic-pnp.herokuapp.com'
21-
AMBIANIC_PNP_PORT = 443
22-
AMBIANIC_PNP_SECURE = True
20+
AMBIANIC_PNP_HOST = 'localhost' # 'ambianic-pnp.herokuapp.com'
21+
AMBIANIC_PNP_PORT = 9779 # 443
22+
AMBIANIC_PNP_SECURE = False # True
2323
time_start = None
2424
peerConnectionStatus = None
2525
discoveryLoop = None
@@ -44,7 +44,6 @@
4444
async def join_peer_room(peer=None):
4545
"""Join a peer room with other local peers."""
4646
# first try to find the remote peer ID in the same room
47-
assert peer
4847
myRoom = PeerRoom(peer)
4948
log.info('Fetching room members...')
5049
peerIds = await myRoom.getRoomMembers()
@@ -168,7 +167,15 @@ async def make_discoverable(peer=None):
168167
while True:
169168
log.info('Making peer discoverable.')
170169
try:
171-
await join_peer_room(peer=peer)
170+
# check if the websocket connection
171+
# to the signaling server is alive
172+
if peer.open:
173+
await join_peer_room(peer=peer)
174+
else:
175+
log.info('Peer not connected to signaling server. '
176+
'Will retry in a bit.')
177+
if peer.disconnected:
178+
await peer.reconnect()
172179
except Exception as e:
173180
log.exception('Unable to join room. '
174181
'Will retry in a few moments. '

src/peerjs/peer.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,11 @@ def on_error(error: str):
204204
self._abort(PeerErrorType.SocketError, error)
205205

206206
@socket.on(SocketEventType.Disconnected)
207-
def on_disconnected():
207+
async def on_disconnected():
208208
if self.disconnected:
209209
return
210210
self.emitError(PeerErrorType.Network, "Lost connection to server.")
211-
self.disconnect()
211+
await self.disconnect()
212212

213213
@socket.on(SocketEventType.Close)
214214
def on_close():
@@ -516,13 +516,14 @@ async def disconnect(self) -> None:
516516
self._id = None
517517
self.emit(PeerEventType.Disconnected, currentId)
518518

519-
def reconnect(self) -> None:
519+
async def reconnect(self) -> None:
520520
"""Attempt to reconnect with the same ID."""
521521
if self.disconnected and not self.destroyed:
522522
log.debug("Attempting reconnection "
523523
f"to server with ID {self._lastServerId}")
524524
self._disconnected = False
525-
self._initialize(self._lastServerId)
525+
self._id = self._lastServerId
526+
await self.start()
526527
elif self.destroyed:
527528
raise RuntimeError("This peer cannot reconnect to the server. "
528529
"It has already been destroyed.")

src/peerjs/socket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async def _connect(self, wss_url=None):
4242
"""Connect to WebSockets server."""
4343
assert wss_url
4444
# connect to websocket
45-
websocket = await websockets.connect(wss_url, ssl=True)
45+
websocket = await websockets.connect(wss_url, ping_interval=5)
4646
self._sendQueuedMessages()
4747
log.debug("WebSockets open")
4848
await websocket.send(

src/peerjs/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def __init__(self):
6464
def validateId(self, id: str = None) -> bool:
6565
"""Ensure alphanumeric ids."""
6666
# Allow empty ids
67-
valid = not id or re.match('^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$', str)
67+
valid = not id or re.match('^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$', id)
6868
log.debug('ID %s is %s valid', id, "" if valid else "not")
6969
return valid
7070

0 commit comments

Comments
 (0)