我能记住 Django 中的对象实例吗?

Can I remember instance of object in Django?

我正在使用 django-channelsaiortc,我想创建 server to peer 连接作为 WebRTC

在我的例子中,如果客户端发送 new-peer 操作,则服务器发送报价。

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

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

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

我也找过session变量,不过好像只能存简单的值,和实例好像也没什么关系

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

consumers.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)

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