我正在关注django channels 2 chat app tutorial
我正在“编写您的第一个消费者”这一部分,我正是按照教程中的内容进行操作, you can find my code here
但是,我仍然无法连接到我的Web套接字!我的错误跟踪:
HTTP GET /chat/ji/ 200 [0.05, 127.0.0.1:51247]
2018-06-23 07:51:43,181 - ERROR - ws_protocol - [Failure instance: Traceback: <class 'ValueError'>: No application configured for scope type 'websocket'
/anaconda3/envs/chatapp/lib/python3.6/site-packages/twisted/internet/defer.py:500:errback
/anaconda3/envs/chatapp/lib/python3.6/site-packages/twisted/internet/defer.py:567:_startRunCallbacks
/anaconda3/envs/chatapp/lib/python3.6/site-packages/twisted/internet/defer.py:653:_runCallbacks
/anaconda3/envs/chatapp/lib/python3.6/site-packages/twisted/internet/defer.py:1442:gotResult
--- <exception caught here> ---
/anaconda3/envs/chatapp/lib/python3.6/site-packages/twisted/internet/defer.py:1384:_inlineCallbacks
/anaconda3/envs/chatapp/lib/python3.6/site-packages/twisted/python/failure.py:422:throwExceptionIntoGenerator
/anaconda3/envs/chatapp/lib/python3.6/site-packages/daphne/server.py:186:create_application
/anaconda3/envs/chatapp/lib/python3.6/site-packages/twisted/python/threadpool.py:250:inContext
/anaconda3/envs/chatapp/lib/python3.6/site-packages/twisted/python/threadpool.py:266:<lambda>
/anaconda3/envs/chatapp/lib/python3.6/site-packages/twisted/python/context.py:122:callWithContext
/anaconda3/envs/chatapp/lib/python3.6/site-packages/twisted/python/context.py:85:callWithContext
/anaconda3/envs/chatapp/lib/python3.6/site-packages/channels/staticfiles.py:41:__call__
/anaconda3/envs/chatapp/lib/python3.6/site-packages/channels/routing.py:58:__call__
]
Not Found: /favicon.ico
[2018/06/23 07:51:43] HTTP GET /favicon.ico 404 [0.03, 127.0.0.1:51249]
[2018/06/23 07:51:47] WebSocket DISCONNECT /ws/chat/ji/ [127.0.0.1:51248]
我无法找到问题所在!
我的主要django项目是chatapp,因为聊天是一个应用程序。
chatapp / routing.py
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import chat.routing
application = ProtocolTypeRouter({
# (http->django views is added by default)
'websocket': AuthMiddlewareStack(URLRouter(chat.routing.websocket_urlpatterns)),
})
聊天/routing.py
from django.conf.urls import url
from . import consumers
websocket_urlpatterns = [
url(r'^ws/chat/(?P<room_name>[^/]+)/$', consumers.ChatConsumer),
]
聊天/consumers.py
from channels.generic.websocket import WebsocketConsumer
import json
class ChatConsumer(WebsocketConsumer):
def connect(self):
self.accept()
def disconnect(self, close_code):
pass
def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json['message']
self.send(text_data=json.dumps({
'message': message
}))
我正在尝试建立网络套接字的聊天室: chat / templates / room.html
<!-- chat/templates/chat/room.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Chat Room</title>
</head>
<body>
<textarea id="chat-log" cols="100" rows="20"></textarea><br/>
<input id="chat-message-input" type="text" size="100"/><br/>
<input id="chat-message-submit" type="button" value="Send"/>
</body>
<script>
var roomName = {{ room_name_json }};
var chatSocket = new WebSocket(
'ws://' + window.location.host +
'/ws/chat/' + roomName + '/');
chatSocket.onopen = function(){
alert('Connection open!');
}
chatSocket.onmessage = function(e) {
var data = JSON.parse(e.data);
var message = data['message'];
document.querySelector('#chat-log').value += (message + '\n');
};
chatSocket.onclose = function(e) {
if(e.code == 1006)
console.error('Chat socket closed unexpectedly');
};
document.querySelector('#chat-message-input').focus();
document.querySelector('#chat-message-input').onkeyup = function(e) {
if (e.keyCode === 13) { // enter, return
document.querySelector('#chat-message-submit').click();
} };
document.querySelector('#chat-message-submit').onclick = function(e) {
var messageInputDom = document.querySelector('#chat-message-input');
var message = messageInputDom.value;
chatSocket.send({
'message': message
});
messageInputDom.value = '';
};
</script>
</html>