首先是一些背景知识,我正在使用本教程: https://blog.heroku.com/in_deep_with_django_channels_the_future_of_real_time_apps_in_django 在Windows 64位上使用Redis的Vagrant分发从这里: https://github.com/ServiceStack/redis-windows 使用Python 3.4和Django 1.10
我能够克隆,构建和运行项目。问题是,当我通过我的浏览器访问django视图时,它加载聊天很好但是当我在控制台中发送消息时它表示websocket连接然后几乎立即断开连接并且消息不发送。如果我手动输入数据到数据库中,而不是在我第一次加载视图时显示的消息,但由于某种原因,javascript应用程序无法保持websocket打开以向广播发送消息。以下是相关代码:
consumers.py
import re
import json
import logging
from channels import Group
from channels.sessions import channel_session
from .models import Room
log = logging.getLogger(__name__)
@channel_session
def ws_connect(message):
# Extract the room from the message. This expects message.path to be of the
# form /chat/{label}/, and finds a Room if the message path is applicable,
# and if the Room exists. Otherwise, bails (meaning this is a some othersort
# of websocket). So, this is effectively a version of _get_object_or_404.
try:
prefix, label = message['path'].strip('/').split('/')
if prefix != 'chat':
log.debug('invalid ws path=%s', message['path'])
return
room = Room.objects.get(label=label)
except ValueError:
log.debug('invalid ws path=%s', message['path'])
return
except Room.DoesNotExist:
log.debug('ws room does not exist label=%s', label)
return
log.debug('chat connect room=%s client=%s:%s',
room.label, message['client'][0], message['client'][1])
# Need to be explicit about the channel layer so that testability works
# This may be a FIXME?
Group('chat-'+label, channel_layer=message.channel_layer).add(message.reply_channel)
message.channel_session['room'] = room.label
@channel_session
def ws_receive(message):
# Look up the room from the channel session, bailing if it doesn't exist
try:
label = message.channel_session['room']
room = Room.objects.get(label=label)
except KeyError:
log.debug('no room in channel_session')
print('no room in channel_session')
return
except Room.DoesNotExist:
log.debug('recieved message, buy room does not exist label=%s', label)
print('recieved message, buy room does not exist label=%s', label)
return
# Parse out a chat message from the content text, bailing if it doesn't
# conform to the expected message format.
try:
data = json.loads(message['text'])
except ValueError:
log.debug("ws message isn't json text=%s", text)
print("ws message isn't json text=%s", text)
return
if set(data.keys()) != set(('handle', 'message')):
log.debug("ws message unexpected format data=%s", data)
print("ws message unexpected format data=%s", data)
return
if data:
log.debug('chat message room=%s handle=%s message=%s',
room.label, data['handle'], data['message'])
print('chat message room=%s handle=%s message=%s',
room.label, data['handle'], data['message'])
m = room.messages.create(**data)
# See above for the note about Group
Group('chat-'+label, channel_layer=message.channel_layer).send({'text': json.dumps(m.as_dict())})
@channel_session
def ws_disconnect(message):
try:
label = message.channel_session['room']
room = Room.objects.get(label=label)
Group('chat-'+label, channel_layer=message.channel_layer).discard(message.reply_channel)
except (KeyError, Room.DoesNotExist):
pass
chat.js
$(function() {
// When we're using HTTPS, use WSS too.
var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
var chatsock = new WebSocket(ws_scheme + '://' + window.location.host + "/chat" + window.location.pathname);
chatsock.onmessage = function(message) {
var data = JSON.parse(message.data);
var chat = $("#chat")
var ele = $('<tr></tr>')
ele.append(
$("<td></td>").text(data.timestamp)
)
ele.append(
$("<td></td>").text(data.handle)
)
ele.append(
$("<td></td>").text(data.message)
)
chat.append(ele)
};
$("#chatform").on("submit", function(event) {
var message = {
handle: $('#handle').val(),
message: $('#message').val(),
}
chatsock.send(JSON.stringify(message));
$("#message").val('').focus();
return false;
});
});
views.py
import random
import string
from django.db import transaction
from django.shortcuts import render, redirect
from haikunator import Haikunator
from .models import Room
def about(request):
return render(request, "chat/about.html")
def new_room(request):
"""
Randomly create a new room, and redirect to it.
"""
haikunator = Haikunator()
new_room = None
while not new_room:
with transaction.atomic():
label = haikunator.haikunate()
if Room.objects.filter(label=label).exists():
continue
new_room = Room.objects.create(label=label)
return redirect(chat_room, label=label)
def chat_room(request, label):
"""
Room view - show the room, with latest messages.
The template for this view has the WebSocket business to send and stream
messages, so see the template for where the magic happens.
"""
# If the room with the given label doesn't exist, automatically create it
# upon first visit (a la etherpad).
room, created = Room.objects.get_or_create(label=label)
# We want to show the last 50 messages, ordered most-recent-last
messages = reversed(room.messages.order_by('-timestamp')[:50])
return render(request, "chat/room.html", {
'room': room,
'messages': messages,
})
models.py
from __future__ import unicode_literals
from django.db import models
from django.utils import timezone
class Room(models.Model):
name = models.TextField()
label = models.SlugField(unique=True)
def __unicode__(self):
return self.label
class Message(models.Model):
room = models.ForeignKey(Room, related_name='messages')
handle = models.TextField()
message = models.TextField()
timestamp = models.DateTimeField(default=timezone.now, db_index=True)
def __unicode__(self):
return '[{timestamp}] {handle}: {message}'.format(**self.as_dict())
# @property
# def formatted_timestamp(self):
# return self.timestamp.strftime('%b %-d %-I:%M %p')
def as_dict(self):
return {'handle': self.handle, 'message': self.message, 'timestamp': self.formatted_timestamp}
我一直在玩javascript,看看我是否可以让websockets保持开放状态,我也试过Simple Websocket Client https://chrome.google.com/webstore/detail/simple-websocket-client/pfdhoblngboilpfeibdedpjgfnlcodoo?hl=en
那些让我相信它与python代码有关的东西也一样吗?
编辑1: 这是javascript应用程序的开发人员控制台 http://imgur.com/AyIGB0z
这是运行daphne后的控制台输出 http://imgur.com/p2yyZiJ
答案 0 :(得分:2)
渠道移动速度非常快,因此您的消费者只需进行一些细微的更改。请注意message.reply_channel.send({'accept': True})
@channel_session
def ws_connect(message):
prefix, label = message['path'].strip('/').split('/')
room = Room.objects.get(label=label)
Group('chat-' + label).add(message.reply_channel)
message.channel_session['room'] = room.label
message.reply_channel.send({'accept': True})
@channel_session
def ws_receive(message):
label = message.channel_session['room']
room = Room.objects.get(label=label)
data = json.loads(message['text'])
m = room.messages.create(handle=data['handle'], message=data['message'])
Group('chat-'+label).send({'text': json.dumps(m.as_dict())})
@channel_session
def ws_disconnect(message):
label = message.channel_session['room']
Group('chat-'+label).discard(message.reply_channel)
此外,当您使用Haikunator
时,您需要定义它的参数。如:
haikunator = Haikunator(
adjectives=['custom', 'adjectives'],
nouns=['custom', 'nouns'],
seed='random seed'
)
haikunator.haikunate(
delimiter='-',
token_length=4,
token_hex=False,
token_chars='0123456789'
)
希望这有帮助。