DEV Community

loading...

Discussion on: How to Add Websockets to a Django App without Extra Dependencies

Collapse
piexpie profile image
Piyush

Hi, I am encountering errrors when creating a websocket connection,

ws = new WebSocket('ws://localhost:8000/')
WebSocket {url: "ws://localhost:8000/", readyState: 0, bufferedAmount: 0, onopen: null, onerror: null, …}
VM50:1 WebSocket connection to 'ws://localhost:8000/' failed: Error during WebSocket handshake: Unexpected response code: 400
Enter fullscreen mode Exit fullscreen mode

And the uvicorn request says: "WARNING: Unsupported upgrade request."

How do I fix this issue?

Thanks.

And also this

ws = new WebSocket('wss://localhost:8000/')
WebSocket {url: "wss://localhost:8000/", readyState: 0, bufferedAmount: 0, onopen: null, onerror: null, …}
ws = new WebSocket('ws://localhost:8000/')
WebSocket {url: "ws://localhost:8000/", readyState: 0, bufferedAmount: 0, onopen: null, onerror: null, …}
ws.onmessage = event => console.log(event.data)
event => console.log(event.data)
ws.send("ping")
VM92:1 Uncaught DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.
    at <anonymous>:1:4
(anonymous) @ VM92:1
ws.send("ping")
VM97:1 Uncaught DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.
    at <anonymous>:1:4
(anonymous) @ VM97:1
VM75:1 WebSocket connection to 'wss://localhost:8000/' failed: WebSocket opening handshake timed out
(anonymous) @ VM75:1
VM82:1 WebSocket connection to 'ws://localhost:8000/' failed: Error during WebSocket handshake: Unexpected response code: 400
Enter fullscreen mode Exit fullscreen mode
Collapse
arjhe profile image
ARJhe

pip install uvicorn
did not deal with WebSocket configuration.

# h11_impl.py L:91
self.ws_protocol_class = config.ws_protocol_class
Enter fullscreen mode Exit fullscreen mode

config.ws_protocol_class is None
so in L:264 you will get a warning.

# h11_impl.py L:264
        if upgrade_value != b"websocket" or self.ws_protocol_class is None:
            msg = "Unsupported upgrade request."
            self.logger.warning(msg)
Enter fullscreen mode Exit fullscreen mode

try: pip install uvicorn[standard]
See ref