我正在尝试使用django-channels为django应用程序创建Web套接字。我已将django更改为使用ASGI
服务器界面而不是默认的WSGI
,并且定义了以下通道层:
settings.py
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'asgi_redis.RedisChannelLayer',
'CONFIG': {
'hosts': [('localhost', 6379)],
},
'ROUTING': 'rest.routing.application',
}
}
ASGI_APPLICATION = 'rest.routing.application'
asgi.py
import os
import django
from channels.routing import get_default_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "rest.settings")
django.setup()
application = get_default_application()
routing.py
application = ProtocolTypeRouter({
"websocket": URLRouter([
url(r"^receive/$", websocket_receive),
url(r"^connect/$", websocket_connect),
])
})
consumers.py
def websocket_connect(self, event):
self.send({
"type": "websocket.accept",
})
问题是,当我尝试对其进行测试
socket = new WebSocket("ws://" + window.location.host + "/connect/");
socket.onopen = function() {
socket.send("hello world");
}
我不断得到
与'ws:// localhost:8000 / connect /'的WebSocket连接失败:错误 在WebSocket握手期间:net :: ERR_CONNECTION_RESET
[Failure instance: Traceback: <class 'AttributeError'>: 'dict' object has no attribute 'send'
C:\Python37\lib\site-packages\autobahn\websocket\protocol.py:2800:processHandshake
C:\Python37\lib\site-packages\txaio\tx.py:429:as_future
C:\Python37\lib\site-packages\twisted\internet\defer.py:151:maybeDeferred
C:\Python37\lib\site-packages\daphne\ws_protocol.py:77:onConnect
--- <exception caught here> ---
C:\Python37\lib\site-packages\twisted\internet\defer.py:151:maybeDeferred
C:\Python37\lib\site-packages\daphne\server.py:186:create_application
C:\Python37\lib\site-packages\channels\staticfiles.py:41:__call__
C:\Python37\lib\site-packages\channels\routing.py:58:__call__
C:\Python37\lib\site-packages\channels\routing.py:144:__call__
C:.......\consumers.py:28:websocket_connect
你知道这里可能出什么问题吗?