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 as manage.py runserver
does. The only difference is that this new server can handle websocket protocol.
To see this ,suppose runserver
runs on 127.0.0.1:8000
and runserver_socketio
on 127.0.0.1:9000
. Just visit 127.0.0.1:9000
and you will get the same webpage with 127.0.0.1:8000
.
The secret lies in django-socketio/django_socketio/example_project/urls.py
, which referencing django-socketio/django_socketio/urls.py
. In this second urls.py
, we can see that it loads events.py
in our project.