Hey everyone can anyone help me with this problem
My application cannot access to the websocket i get an error in Dev console
(index):346 WebSocket connection to 'wss://happy-moment.live:8001/search_game/' failed:
(anonymous) @ (index):346
(index):380 WebSocket error observed: Event {isTrusted: true, type: 'error', target: WebSocket, currentTarget: WebSocket, eventPhase: 2, …}
All my services working fine(nginx, redis, gunicorn, daphne) I am using this versions of packages:
daphne==3.0.2
Django==4.2.5
channels==3.0.5
my website conf file is
server {
server_name happy-moment.live;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ftpuser/main;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
location /ws/ {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_pass http://127.0.0.1:8001;
}
}
also i tried to listen 80 and redirect to listen 443 with specifying ssl certs
But i get an error
ERR_TOO_MANY_REDIRECTS
So that’s why i removed the http redirects to https
Website works fine in https and http (because i installed ssl using my vps panel)
here is my daphne service
[Unit]
Description=WebSocket Daphne Service
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/home/ftpuser/main
ExecStart=/home/ftpuser/venv/bin/python /home/ftpuser/venv/bin/daphne -e ssl:8001:privateKey=/etc/ssl/private.key:certKey=/etc/ssl/certificate.crt main.asgi:application
Restart=on-failure
[Install]
WantedBy=multi-user.target
Note: Also tried without specify privateKey and certKey
Redis service also listening port “127.0.0.1”, 6379
Here is my settings.py
INSTALLED_APPS = [
'daphne',
'chat',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'channels',
]
ASGI_APPLICATION = 'chat.routing.application'
WSGI_APPLICATION = 'main.wsgi.application'
CHANNEL_LAYERS = {
# 'default': {
# 'BACKEND': 'channels.layers.InMemoryChannelLayer',
# },
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("127.0.0.1", 6379)],
},
},
}
my main/asgi.py
import os
import django
from channels.routing import get_default_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings')
django.setup()
application = get_default_application()
and my chat/routing.py
from channels.auth import AuthMiddlewareStack
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import path
from channels.security.websocket import AllowedHostsOriginValidator
from django.urls import re_path
from . import consumers
application = ProtocolTypeRouter({
'http': get_asgi_application(),
'websocket':AllowedHostsOriginValidator(
AuthMiddlewareStack(
URLRouter([
re_path(r'/chat/(?P<pk>\d+)/$', consumers.GameConsumer.as_asgi()),
path('search_game/', consumers.SearchGameConsumer.as_asgi()),
])
)
)
})
Can anyone help me with this problem?