You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django Daphne安全WebSocket(wss)连接失败求助

WSS连接失败排查求助

已将Django服务器配置为HTTPS模式,API调用可正常运行;Daphne配置完成后启动无异常,但通过wss协议建立安全WebSocket连接时失败,寻求技术支持。

相关配置及日志信息

docker-compose.yml

services:
  api:
    platform: linux/amd64
    container_name: api
    build:
      context: .
      dockerfile: Dockerfile.dev
    command: 'sh -c "./manage.py migrate && python manage.py runsslserver 0.0.0.0:8080"'
    restart: always
    networks:
      - default
    volumes:
      - ./:/app
      - $HOME/.aws:/root/.aws:ro
      - /var/run/docker.sock:/var/run/docker.sock
      - type: bind
        source: ./docker/cpuinfo_with_fake_speed.txt
        target: /proc/cpuinfo
    ports:
      - 8080:8080
    env_file:
      - ./.env
    depends_on:
      - db

  daphne:
    platform: linux/amd64
    container_name: daphne
    build:
      context: .
      dockerfile: Dockerfile.dev
    command: 'sh -c "daphne  --ws-protocol [ws, wss] -v 1 -e ssl:8000:privateKey=key.pem:certKey=cert.pem apps.asgi:application"'
    restart: always
    working_dir: /app
    networks:
      - default
    volumes:
      - ./:/app
      - /var/run/docker.sock:/var/run/docker.sock
    ports:
      - 8000:8000

asgi.py

django_asgi_app = get_asgi_application()
from channels.routing import ProtocolTypeRouter, URLRouter

application = ProtocolTypeRouter(
    {
        "http": django_asgi_app,
        "websocket": AuthMiddlewareStack(URLRouter([path('ws/notifications/', NotificationConsumer.as_asgi())])),
    }
)

Daphne服务启动日志

daphne    | 2023-04-28 05:39:32,845 INFO     Starting server at ssl:8000:privateKey=key.pem:certKey=cert.pem
daphne    | 2023-04-28 05:39:32,847 INFO     HTTP/2 support enabled
daphne    | 2023-04-28 05:39:32,847 INFO     Configuring endpoint ssl:8000:privateKey=key.pem:certKey=cert.pem
daphne    | 2023-04-28 05:39:32,891 INFO     Listening on TCP address 0.0.0.0:8000

尝试连接的WSS地址

wss://localhost:8000/ws/notifications/?userid=x48

内容的提问来源于stack exchange,提问作者Neo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 16:52:35