有一个使用网络套接字和通道的Django聊天应用程序,你可以看到wecbokect连接:
[mansard@web569 src]$ python3.5 manage.py runserver 22783 Performing system checks...
Django version 1.11, using settings 'chatbot.settings'
2017-09-04 16:53:05,478 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2017-09-04 16:53:05,479 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2017-09-04 16:53:05,479 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2017-09-04 16:53:05,480 - INFO - server - HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2017-09-04 16:53:05,480 - INFO - server - Using busy-loop synchronous mode on channel layer
2017-09-04 16:53:05,480 - INFO - server - Listening on endpoint tcp:port=22783:interface=127.0.0.1
[2017/09/04 16:54:09] WebSocket HANDSHAKING /webscok/ [127.0.0.1:42846]
[2017/09/04 16:54:09] WebSocket CONNECT /webscok/ [127.0.0.1:42846]
chat.js:
$(window).load(function() {
$messages.mCustomScrollbar();
var ws_path = "/webscok/";
var socket = new WebSocket("ws://" + window.location.host + ws_path);
socket.onmessage = function(e) {chatMessage(e.data);}
$('.message-submit').click(function() {
msg = $('.message-input').val();
socket.send(msg);
insertMessage(msg);});
$(window).on('keydown', function(e) {
if (e.which == 13) {
msg = $('.message-input').val();
socket.send(msg);
insertMessage(msg);}});
setTimeout(function() {
welcomingMessage();}, 100);
});
consumers.py:
import json
from channels import Channel , Group
from channels.auth import http_session_user, channel_session_user, channel_session_user_from_http
from channels.sessions import channel_session
def ws_connect(message):
message.reply_channel.send({"accept": True})
Group('chat').add(message.reply_channel)
def ws_receive(message):
Group('chat').send({"text": message.content['text'],})
def ws_disconnect(message):
Group('chat').discard(message.reply_channel)
routing.py:
from channels.routing import route
from Pchat.consumers import ws_connect, ws_disconnect, ws_receive
channel_routing = [
route("websocket.connect", ws_connect,path=r"^/webscok"),
route("websocket.disconnect", ws_disconnect),
route("websocket.receive", ws_receive),
]
settings.py:
CHANNEL_LAYERS = {
"default": {
"BACKEND": "asgi_redis.RedisChannelLayer",
"CONFIG": {
"hosts": ['unix:///home/mansard/webapps/gadgetron/var/redis.sock',],},
"ROUTING": "chatbot.routing.channel_routing",
},
}
cht.html:
{% load static from staticfiles %}
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>المساعد الذكي</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<link rel='stylesheet prefetch' href='https://fonts.googleapis.com/css?family=Open+Sans'>
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/malihu-custom-scrollbar-plugin/3.1.3/jquery.mCustomScrollbar.min.css'>
<link rel="icon" href="/static/icon.png">
<link rel="favicon.ico" href="/static/icon.png">
<link rel="stylesheet" href="/static/css/chatbot_style.css">
</head>
<body>
<div class="chat">
<div class="chat-title">
<h1>محمد .. online now</h1>
<h2>الـمـسـاعـد الـذكـي</h2>
<figure class="avatar">
<img src="/static/icon.png" /></figure>
</div>
<div class="messages">
<div class="messages-content"></div>
</div>
<div class="message-box">
<textarea type="text" class="message-input" placeholder="اكتب سؤالك هنا"></textarea>
<button type="submit" class="message-submit">ارسال</button>
</div>
</div>
<div class="bg"></div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/malihu-custom-scrollbar-plugin/3.1.3/jquery.mCustomScrollbar.concat.min.js'></script>
<script src="/static/js/channels/js/websocketbridge.js"></script>
<script src="/static/js/PP_chat_index.js"></script>
我试着回复消息给我,它完美地工作。现在我开始创建一个聊天服务器,将新客户端添加到聊天组并将所有传入消息广播到所有连接的客户端,我已经配置并安装了所有内容,但Group()不起作用,没有任何东西在干扰,没有错误已经显示出来,所以我不知道问题出在哪里?