When runserver to start django, some websocket connections are automatically interrupted

created at 07-15-2021 views: 12

When runserver to start django, some websocket connections are automatically interrupted.

Error status code

When the WebSocket is disconnected, a CloseEvent will be triggered, and the CloseEvent will be sent to the client using WebSockets when the connection is closed. It is used in the onclose event listener of the WebSocket object. The code field of CloseEvent indicates the reason for the WebSocket disconnection. The reason for disconnection can be analyzed from this field.

  • CloseEvent.code:  error code, which is an integer type
  • CloseEvent.reason: disconnect reason, which is a string
  • CloseEvent.wasClean: indicates whether it is disconnected normally, a boolean value. In general abnormal disconnection, the value is false
status codenamedescription
0–999 Reserved segment, unused.
1000CLOSE_NORMALNormally closed; no matter what the purpose was created, the link has successfully completed the task.
1001CLOSE_GOING_AWAYThe terminal leaves, either because of a server error, or because the browser is jumping away from the page that opened the connection.
1002CLOSE_PROTOCOL_ERRORThe connection was interrupted due to a protocol error.
1003CLOSE_UNSUPPORTEDThe connection was disconnected due to receiving an unallowable data type (for example, a terminal that only received text data received binary data).
1004 Reserved. Its meaning may be defined in the future.
1005CLOSE_NO_STATUSReserved. Indicates that the expected status code was not received.
1006CLOSE_ABNORMALReserved. Used to expect the connection to close abnormally when the status code is received (that is, no close frame is sent).
1007Unsupported DataThe connection was disconnected due to received data that does not match the format (e.g. non-UTF-8 data is included in the text message).
1008Policy ViolationThe connection was disconnected due to receipt of data that did not conform to the agreement. This is a general status code used in scenarios where the 1003 and 1009 status codes are not suitable.
1009CLOSE_TOO_LARGEThe connection was disconnected due to an excessively large data frame.
1010Missing ExtensionThe client expects the server to agree on one or more extensions, but the server does not handle it, so the client disconnects.
1011Internal ErrorBecause the client encountered an unexpected situation that prevented it from completing the request, the server disconnected.
1012Service RestartThe server was disconnected due to restart.
1013Try Again LaterThe server is disconnected due to temporary reasons, such as the server is overloaded and some client connections are disconnected.
1014 Reserved by the WebSocket standard for future use.
1015TLS HandshakeReserved. Indicates that the connection was closed because the TLS handshake could not be completed (for example, the server certificate could not be verified).
1016–1999 Reserved by the WebSocket standard for future use.
2000–2999 Reserved for use by the WebSocket extension.
3000–3999 Can be used by libraries or frameworks.? Should not be used by apps. Can be registered with IANA, first come first served.
4000–4999 Can be used by the app.


method 1: use uvicorn

python -m pip install uvicorn

Run in uvicorn:

uvicorn myproject.asgi:application

If you encounter an error, Django does not support websocket. You can try to copy asgi.py to the same directory as setting.py.

method2: use daphne

python -m pip install daphne

Run in daphne:

daphne myproject.asgi:application
created at:07-15-2021
edited at: 07-15-2021: