Hello, I am encountering an error.
Despite trying various methods and researching previous discussions here, I cannot seem to resolve the issue. Whether I use daphne or uvicorn, the result remains the same - it works locally but fails on the server with the following error:
"Django can only handle ASGI/HTTP connections, not WebSockets."
My Code:
JS Code:
<script>
function connectSocket() {
var ws = new WebSocket('ws://' + window.location.host + '/ws/notification/{{request.user.id}}/')
ws.onopen = function(event){
console.log('opened', event);
}
// Long onmessage codes omitted for brevity
ws.onclose = function(e) {
console.error('Chat socket closed unexpectedly; reconnecting');
setTimeout(connectSocket, 1000);
};
ws.onerror = function(event){
console.log('error', event);
}
// window.setInterval(function(){
// loadNotifications();
// }, 1000);
}
connectSocket();
</script>
Settings.py
[/JSR]
[CODE]
CHANNEL_LAYERS = {
"default": {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [("redis", 6379)],
},
},
}
ASGI_APPLICATION = "thesite.routing.application"
asgi
import os
import django
from channels.routing import get_default_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'thesite.settings')
django.setup()
application = get_default_application()
Nginx:
server {
listen 80;
server_name **.net www.**.net; #** = site name
root /home/ftpuser/project;
location /static/ {
alias /home/ftpuser/project/site/static/;
}
location /media/ {
}
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://unix:/home/ftpuser/project/thesite.sock;
}
location /ws/ {
proxy_pass http://0.0.0.0:9001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}