问题
First some background, I am working off of this tutorial: https://blog.heroku.com/in_deep_with_django_channels_the_future_of_real_time_apps_in_django On Windows 64-bit using the Vagrant distribution of Redis from here: https://github.com/ServiceStack/redis-windows Using Python 3.4 and Django 1.10
I am able to clone, build and run the project fine. the problem is that when I access the django view through my browser it loads the chat fine but when I send a message in the console it says the websocket connects then almost immediately disconnects and the message does not send. If I manually enter data into the database than the messages will show up when i first load the view but for some reason the javascript application cannot keep the websocket open to send messages to broadcast. Below is the relevant code:
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}
I have been playing around with the javascript to see if I can get the websockets to stay open, I have also tried Simple Websocket Client https://chrome.google.com/webstore/detail/simple-websocket-client/pfdhoblngboilpfeibdedpjgfnlcodoo?hl=en
and that did the same thing those that leads me to believe it's something to do with the python code?
Edit 1: Here is the developer console of the javascript appliction http://imgur.com/AyIGB0z
and this is the console output after running daphne http://imgur.com/p2yyZiJ
回答1:
Channels moves pretty quickly, so there's just some minor changes in your consumers. Note the bottom line of message.reply_channel.send({'accept': True})
consumers.py
@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)
Also, when you're using Haikunator
you need to define it's parameters. Such as:
haikunator = Haikunator(
adjectives=['custom', 'adjectives'],
nouns=['custom', 'nouns'],
seed='random seed'
)
haikunator.haikunate(
delimiter='-',
token_length=4,
token_hex=False,
token_chars='0123456789'
)
Hope this helps.
来源:https://stackoverflow.com/questions/42615905/django-channels-websockets-connecting-and-disconnecting-right-away