0

我正在使用django-channelsand aiortc,我想将server to peer连接创建为WebRTC.

就我而言,如果客户端发送new-peer操作,则服务器发送报价。

在客户收到报价后,客户将答案作为new-answer操作发送。

但是服务器不记得peer了,所以不能setRemoteDescription

当然我知道服务器不记得了,但是我该如何处理服务器需要记住像我这样的实例的情况呢?

我也找了session变量,但是好像只能存储简单的值,和实例好像没什么关系。

请提供与此相关的有用文档或解决方案。

consumer.receive 方法:

    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)
4

1 回答 1

0

已解决:django session 支持将实例保存到他们的 session。

于 2021-11-19T05:49:25.940 回答