django / daphne

Django Channels HTTP/WebSocket server
BSD 3-Clause "New" or "Revised" License
2.32k stars 256 forks source link

Websocket client does no receive any pings #513

Open liangpengfei opened 1 week ago

liangpengfei commented 1 week ago

I have configured a simple demo project according to the Channel documentation. I hope that after the client successfully connects, it will receive a ping message sent from the server to the client every 20 seconds. However, the actual situation is that no messages are being sent to the client.

I use this website to be a client. https://websocketking.com/

Your OS and runtime environment, and browser if applicable

Mac OS m1

A pip freeze output showing your package versions

asgiref==3.8.1
attrs==23.2.0
autobahn==23.6.2
Automat==22.10.0
cffi==1.16.0
channels==4.1.0
constantly==23.10.4
cryptography==42.0.8
daphne==4.1.2
Django==5.0.6
hyperlink==21.0.0
idna==3.7
incremental==22.10.0
pyasn1==0.6.0
pyasn1_modules==0.4.0
pycparser==2.22
pyOpenSSL==24.1.0
service-identity==24.1.0
six==1.16.0
sqlparse==0.5.0
Twisted==24.3.0
txaio==23.1.1
typing_extensions==4.12.2
zope.interface==6.4.post2

What you expected to happen vs. what actually happened

client receives a ping message sent from the server every 20 seconds.

How you're running Channels (runserver? daphne/runworker? Nginx/Apache in front?)

daphne -b 0.0.0.0 -p 8000  ChannelsDemo.asgi:websocket_application

Console logs and full tracebacks of any errors

no error logs, just not working.

Code

asgi.py

import os

from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
from django.urls import re_path

from ChannelsDemo.consumer import YourConsumer

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ChannelsDemo.settings")

django_asgi_app = get_asgi_application()

websocket_application = ProtocolTypeRouter({
    "http": django_asgi_app,
    "websocket": URLRouter([
        re_path(r'^api/v1/ws/bi/chat/', YourConsumer.as_asgi()),
    ]),
})

consumer.py

import json
import time

from channels.generic.websocket import WebsocketConsumer

class YourConsumer(WebsocketConsumer):
    def connect(self):
        self.accept()
        self.send(text_data=json.dumps({
            'message': 'Hello from server!'
        }))

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

    def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']

        self.send(text_data=json.dumps({
            'message': message}))

settings.py

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-us2)u&pkq=(9w0@q_f!2gq8_j13mp)prv!qfo6ggvu=+z0onc#"

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = [
    "daphne",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "channels"
]

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "ChannelsDemo.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [BASE_DIR / 'templates']
        ,
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

# WSGI_APPLICATION = "ChannelsDemo.wsgi.application"
ASGI_APPLICATION = 'ChannelsDemo.asgi.websocket_application'

# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "db.sqlite3",
    }
}

# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
    },
]

# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/

STATIC_URL = "static/"

# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"