我使用django-channels
和aiortc
,我想将server to peer
连接创建为WebRTC
。
在我的情况下,如果客户端发送new-peer
操作,则服务器发送报价。
在客户收到报价后,客户将答案作为new-answer
动作发送。
但服务器记不住peer
,所以记不起setRemoteDescription
。
当然,我知道服务器不记得了,但我该如何处理服务器需要记住像我这样的实例的情况?
我还查找了会话变量,但似乎只有简单的值可以存储,而且它似乎与实例无关。
请提供与此相关的有用文档或解决方案。
消费者接收方式:
async def receive(self, text_data):
receive_dict = json.loads(text_data)
message = receive_dict['message']
action = receive_dict['action']
if (action == 'new-peer'):
# Create Offer
peer, offer = await create_offer()
receive_dict['message']['sdp'] = offer
receive_dict['action'] = 'new-offer'
receive_dict['message']['receiver_channel_name'] = self.channel_name
await self.channel_layer.send(
self.channel_name,
{
'type': 'send.sdp',
'receive_dict': receive_dict
}
)
return None
elif (action == 'new-answer'):
await peer.setRemoteDescription(receive_dict['message']['sdp'])
receiver_channel_name = receive_dict['message']['receiver_channel_name']
receive_dict['message']['receiver_channel_name'] = self.channel_name
await self.channel_layer.send(
receiver_channel_name,
{
'type': 'send.sdp',
'receive_dict': receive_dict,
}
)
return None
else:
raise ValueError('Unexpected action value');
create_offer函数:
async def create_offer():
pc = RTCPeerConnection(RTCConfiguration(iceServers))
rtsp_video = MediaPlayer(rtsp_test_url)
relay = MediaRelay()
pc.addTrack(relay.subscribe(rtsp_video.video))
@pc.on("connectionstatechange")
async def on_connectionstatechange():
print("Connection state is %s", pc.connectionState)
if pc.connectionState == "failed":
await pc.close()
await pc.setLocalDescription(await pc.createOffer())
return pc, object_to_string(pc.localDescription)
已解决:django会话支持将实例保存到会话中。