【问题标题】:Can I remember instance of object in Django?我可以记住 Django 中的对象实例吗?
【发布时间】:2021-12-29 07:10:08
【问题描述】:

我正在使用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 django-channels aiortc


    【解决方案1】:

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

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2012-07-08
    • 2015-08-06
    • 1970-01-01
    • 2012-08-05
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    相关资源
    最近更新 更多