uwsgi
#dwebsocket作者在github详细说明了配置要求:https://github.com/duanhongyi/dwebsocket #If you want to use the uwsgi backend, add WEBSOCKET_FACTORY_CLASS in the settings.py file: WEBSOCKET_FACTORY_CLASS = 'dwebsocket.backends.uwsgi.factory.uWsgiWebSocketFactory' #Run uwsgi: uwsgi --http :8080 --http-websockets --processes 1 --wsgi-file wsgi.py--async 30 --ugreen --http-timeout 300
以下,均是我测试过程中碰到的报错:
WebSocket connection to ‘ws://*****’ failed: Error during WebSocket handshake: Unexpected response code: 400
#Solution #在django的setting.py添加: WEBSOCKET_FACTORY_CLASS = 'dwebsocket.backends.uwsgi.factory.uWsgiWebSocketFactory'
正常连接websocket(通过request.is_websocket()判断为True),Status Code为101,但django收不到消息(request.websocket没有数据)。
#Solution 我遇到这种情况大部分是uwsgi配置有问题,以下是我遇上这个问题的解决办法 1.在配置文件uwsgi.ini添加 http-websockets = True 2.错误配置了enable-threads 在配置文件uwsgi.ini删除或注释 #enable-threads = True 就可以收到消息了
no app loaded. going in full dynamic mode
#Solution sudo apt-get remove uwsgi sudo apt-get update #创建并进入虚拟环境 sudo pip install uwsgi
uwsgi ASYNC call without async mode !!!
#Solution #threads = 3 #删掉! async = 30 ugreen = True
nginx
需求是配置成https访问websocket,均是我测试过程中碰到的报错:
WebSocket connection to ‘*****’ failed: Error in connection
establishment: net::ERR_SSL_PROTOCOL_ERROR
#Solution #websocket 连接的域名不支持wss协议,那就用nginx配置个wss。 nginx添加如下(最简配置): location /wss { proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_pass http://***; #代理到你的服务地址 }
Mixed Content: The page at ‘’ was loaded over HTTPS, but
attempted to connect to the insecure WebSocket endpoint 'ws://’.
This request has been blocked; this endpoint must be available over
WSS.
Uncaught DOMException: Failed to construct ‘WebSocket’: An
insecure WebSocket connection may not be initiated from a page loaded
over HTTPS.
#Solution 报错写的很清楚,当前页面配置为https,但客户端试图连接到不安全的websocket端点'ws://',请求被阻止,所以客户端请求改成'wss://'就好了 wss协议实际是websocket+SSL,就是在websocket协议上加入SSL层,类似https:http+SSL。