I can't make Apache serve websockets through Daphne.
The requirements.txt
Django==2.0.7 channels==2.1.3 asgiref==2.3.2 redis==2.10.6
settings.py
CHANNEL_LAYERS = {
"default":{
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("localhost"), 6379],
},
"ROUTING": "myapp.routing.channel_routing",
},
}
ASGI_APPLICATION = "myapp.routing.application"
USE_WEBSOCKETS = True
routing.py
#..imports
websocket_urlpatterns = [
url(r"", MyConsumer),
]
application = ProtocolTypeRouter({
'websocket': AllowedHostsOriginValidator(
AuthMiddlewareStack(
URLRouter(
websocket_urlpatterns
)
)
)
})
asgi.py
#.. imports
os.environment.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
django.setup()
application = get_default_application()
MyConsumer.py
#..imports
class MyConsumer(WebsocketConsumer):
def websocket_connect(self, event):
Logger.log("Connected " + json.dumps(event))
self.send({
"type": "websocket.accept"
})
def websocket_receive(self, event):
Logger.log("Receive " + json.dumps(event))
def websocket_disconnect(self, event):
Logger.log("Disconnected " + json.dumps(event))
httpd.conf
#...
RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC, OR]
RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
RewriteRule .* ws://127.0.0.1:9001%{REQUEST_URI} [P, QSA, L]
supervisord.conf
;...
[fcgi-program:asgi]
socket=tcp://127.0.0.1:9001
command=/var/www/venv/bin/daphne -u /run/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-headers myapp.asgi.application
numprocs=2
process_name=asgi%(process_num)d
directory=/var/www/venv/myapp/
autostart=true
autorestart=true
stdout_logfile=/tmp/asgi.log
redirect_stderr=true
In javascript, I just try to connect:
var location = window.location
var socket = new WebSocket('ws://' + location.host + ':9001' + location.pathname)
socket.onopen = function(e){
console.log('open', e)
}
socket.onerror = function(e){
console.log('error', e)
}
socket.onclose = function(e){
console.log('close', e)
}
Each time the page is accessed it gives WebSocket connection to 'ws://127.0.0.1:9001/' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET
after a while and prints the error and close messages in console.
I followed the example setup provided at readthedocs and used an nginx instance, but it gives the same error.
It seems that the message is not received by the backend, so I suppose there must be something in the setup, namely with the messaging queue or redis server. What am I missing?
Yeah i sped quite a bit troubleshooting a setup simmilar to yours. I am currently running channels2, daphne,redis and apache together on a centos 7.5 server. Finally working perfectly.
I find troubleshooting a bit easier using AsyncConsumer, gives a bit more control. Try doing :
from channels.consumer import SyncConsumer, AsyncConsumer
class ChatConsumer(AsyncConsumer):
async def websocket_connect(self, event):
# when the socket connects
print(event)
await self.send({
"type": "websocket.accept"
})
then you can see the data comming in, if any.
And for apache, like you, I use the rewrite engine. (in the vhost file)
RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC,OR]
RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
RewriteRule .* ws://127.0.0.1:9001%{REQUEST_URI} [P,QSA,L]
Hope you figure it out ! /R
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.