与 Django Channels 的 React 本机 Websocket 连接已打开,但没有消息通过

React native Websocket connection with Django Channels is open but no messages get through

我有一个与 Django 通道通信的 React 本机应用程序 (android)。通过 ws 连接到我的开发服务器时,一切正常。 但是,当我尝试使用相同的代码连接到我的远程 wss 服务器时,没有任何通过。

套接字连接显示为 OPEN。我什至收到了从服务器发送到我的应用程序的第一条 "connected" 消息:

class RGConsumer(AsyncWebsocketConsumer):
    rooms = []

    async def connect(self):
        self.rooms = []
        await self.join_room('all')
        await self.accept()
        await self.send(text_data=json.dumps({'event': 'connected'}))

    async def join_room(self, room_name):
        if room_name not in self.rooms:
            self.rooms.append(room_name)
            await self.channel_layer.group_add(
                room_name,
                self.channel_name
            )

问题是除了第一条消息之外,没有其他消息通过。 例如,无论我通过此功能发送什么,应用程序都不会收到:

def send_to_all(event, data=None):
    message = {'type': 'channel_event', 'message': {'event': event, 'data': data}}
    channel_layer = get_channel_layer()
    async_to_sync(channel_layer.group_send)(
        'all',
        message
    )

以同样的方式,当我在我的应用程序上调用 websocket.send 时,我的消费者的 receive 功能根本没有被触发。

再一次,这在我的本地服务器上工作得很好,所以我假设代码是正确的。只有当应用程序连接到生产 wss 服务器时,它才会停止工作(除了第一条 connected 收到的消息)

此外,如果我决定重新启动我的生产服务器,即使 websocket.onclose 函数也不会被调用。

是否有任何其他设置允许正确连接到我可能会丢失的 wss 服务器?

如果需要更多代码,请告诉我。我不确定还需要什么。

我设法通过为移动 WebSocket 连接创建不同的通道路由来解决这个问题。

(我的网站 /ws/ 应用程序 /mobile-ws/

我仍然不知道为什么使用与我的网站相同的端点会出现问题,或者为什么它适用于开发而不适用于生产。我很乐意接受比这更好的答案。