JavaScript
x
5
1
from aiortc import RTCPeerConnection
2
3
pc = RTCPeerConnection()
4
print(pc.connectionState)
5
Error:
JavaScript
1
6
1
C:UsersΧρήστοςDesktop>python 123.py
2
Traceback (most recent call last):
3
File "123.py", line 4, in <module>
4
print(pc.connectionState)
5
AttributeError: 'RTCPeerConnection' object has no attribute 'connectionState'
6
It’s the first property in the documentation of aiortc but i cannot retrieved it.
Note: I also tried with connectionstatechange
event but either that worked.
Edit: That’s the methods the pc
object has:
JavaScript
1
2
1
['_RTCPeerConnection__assertNotClosed', '_RTCPeerConnection__assertTrackHasNoSender', '_RTCPeerConnection__connect', '_RTCPeerConnection__createDtlsTransport', '_RTCPeerConnection__createSctpTransport', '_RTCPeerConnection__createTransceiver', '_RTCPeerConnection__gather', '_RTCPeerConnection__getTransceiverByMLineIndex', '_RTCPeerConnection__getTransceiverByMid', '_RTCPeerConnection__localDescription', '_RTCPeerConnection__localRtp', '_RTCPeerConnection__remoteDescription', '_RTCPeerConnection__remoteRtp', '_RTCPeerConnection__setSignalingState', '_RTCPeerConnection__updateIceConnectionState', '_RTCPeerConnection__updateIceGatheringState', '_RTCPeerConnection__validate_description', '__class__', '__delattr__', '__dir__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_add_event_handler', '_call_handlers', '_emit_handle_potential_error', '_emit_run', 'addIceCandidate', 'addTrack', 'addTransceiver', 'close', 'createAnswer', 'createDataChannel', 'createOffer', 'emit', 'getReceivers', 'getSenders', 'getStats', 'getTransceivers', 'listeners', 'on', 'once', 'remove_all_listeners', 'remove_listener', 'setLocalDescription', 'setRemoteDescription']
2
Advertisement
Answer
aiortc
uses pyee
for making aiortc
event-driven and as you can see in the official GitHub example (webcam/webcam.py), you must put an event listener on an event named iceconnectionstatechange
.
Checkout the code snippet below:
JavaScript
1
13
13
1
from aiortc import RTCPeerConnection
2
3
4
pc = RTCPeerConnection()
5
6
7
@pc.on("iceconnectionstatechange")
8
async def on_iceconnectionstatechange():
9
print(f"ICE connection state is {pc.iceConnectionState}")
10
11
if pc.iceConnectionState == "failed":
12
# Do something
13
In your question you mentioned that you’ve tried connectionstatechange
event but it’s actually iceconnectionstatechange
and that’s why it didn’t work the first time.