I'm writing a realtime chatroom similar to this package with Django. It runs a separate WebSocket server with command
python manage.py runserver_socketio
But I can't figure out how to make the runserver_socketio
command load my handler. The only related code I can find in the package is here in django-socketio/django_socketio/management/commands/runserver_socketio.py
:
server = SocketIOServer(bind, handler, resource="socket.io")
....
handler = WSGIHandler()
But why on earth is this handler
related to my code?
I got it. the
manage.py runserver_socketio
command starts almost an identical server asmanage.py runserver
does. The only difference is that this new server can handle websocket protocol.To see this ,suppose
runserver
runs on127.0.0.1:8000
andrunserver_socketio
on127.0.0.1:9000
. Just visit127.0.0.1:9000
and you will get the same webpage with127.0.0.1:8000
.The secret lies in
django-socketio/django_socketio/example_project/urls.py
, which referencingdjango-socketio/django_socketio/urls.py
. In this secondurls.py
, we can see that it loadsevents.py
in our project.