Django频道,Daphne和Docker:当第一个连接的客户端断开连接时,客户端停止接收消息

在尝试使用Daphne服务器在Docker上部署Django通道时,我遇到了问题。特别是,当第一个连接的客户端断开连接时,所有客户端都停止接收消息。此外,还会发生以下异常:

  ERROR    Exception in callback AsyncioSelectorReactor.callLater.<locals>.run() at /usr/local/lib/python3.8/site-packages/twisted/internet/asyncioreactor.py:287
daphne_1        | handle: <TimerHandle when=106251.082809453 AsyncioSelectorReactor.callLater.<locals>.run() at /usr/local/lib/python3.8/site-packages/twisted/internet/asyncioreactor.py:287>
daphne_1        | Traceback (most recent call last):
daphne_1        |   File "/usr/local/lib/python3.8/asyncio/events.py",line 81,in _run
daphne_1        |     self._context.run(self._callback,*self._args)
daphne_1        |   File "/usr/local/lib/python3.8/site-packages/twisted/internet/asyncioreactor.py",line 290,in run
daphne_1        |     f(*args,**kwargs)
daphne_1        |   File "/usr/local/lib/python3.8/site-packages/daphne/server.py",line 270,in application_checker
daphne_1        |     exception = application_instance.exception()

routing.py:

# chat/routing.py
from django.urls import re_path

from . import consumers

websocket_urlpatterns = [
    re_path(r'ws/chat/(?P<room_name>\w+)/$',consumers.ChatConsumer),]

consumers.py:

# chat/consumers.py
from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer
import json

class ChatConsumer(WebsocketConsumer):
    def connect(self):
        print("connect")

        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = 'chat_%s' % self.room_name

        # Join room group
        async_to_sync(self.channel_layer.group_add)(
            self.room_group_name,self.channel_name
        )

        self.accept()

    def disconnect(self,close_code):
        print("disconnect")

        # Leave room group
        async_to_sync(self.channel_layer.group_discard)(
            self.room_group_name,self.channel_name
        )

    # Receive message from WebSocket
    def receive(self,text_data):
        print("receive")

        text_data_json = json.loads(text_data)
        message = text_data_json['message']

        # Send message to room group
        async_to_sync(self.channel_layer.group_send)(
            self.room_group_name,{
                'type': 'chat_message','message': message
            }
        )

    # Receive message from room group
    def chat_message(self,event):
        message = event['message']

        # Send message to WebSocket
        self.send(text_data=json.dumps({
            'message': message
        }))

asgi.py:

import os
import django
from channels.routing import get_default_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE","django_test_2.settings")
django.setup()
application = get_default_application()

settings.py渠道层:

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer','CONFIG': {
            "hosts": [('redis',6379)],},}

Dockerfile:

FROM python:3

RUN pip install daphne==2.4.0

RUN mkdir /test
WORKDIR /test
COPY requirements.txt /test/
RUN pip install -r requirements.txt
COPY . /test/

docker-compose.yml:

version: '3.7'

services:

  redis:
    image: "redis"
    restart: on-failure
    networks:
      - external
    ports:
      - "6379:6379"

  daphne:
    build:
      context: .
      dockerfile: Dockerfile_daphne
    command: bash -c "daphne -b 0.0.0.0 -p 8000 django_test_2.asgi:application"
    ports:
      - "8449:8000"
    networks:
      - external

networks:
  external:
    external: true

请,您能帮忙吗?

woshilaofu 回答:Django频道,Daphne和Docker:当第一个连接的客户端断开连接时,客户端停止接收消息

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2970111.html

大家都在问