我正在使用Django和Channels(Django的新websocket实现)编写聊天应用程序。用户首先进入候诊室,直到至少有3个人加入,然后他们被引导到聊天室。该代码适用于Chrome,但对Firefox无效。以下是它的工作原理:
以下是调试问题所采取的步骤:
基于这些调试步骤,我认为当Firefox启动执行组重定向的服务器代码时会出现问题。你有什么可能出错的建议吗?你有什么建议可以帮助你找出问题吗?
以下是我认为导致问题的consumer.py中的代码:
@channel_session_user_from_http
@channel_session
def wait_connect(message):
try:
wait_room, created = WaitRoom.objects.get_or_create(title=title)
except WaitRoom.DoesNotExist:
log.debug('ws room does not exist title=%s', title)
return
Group('wait-'+title, channel_layer=message.channel_layer).add(message.reply_channel)
wait_user,created = wait_room.users.get_or_create(user=message.user)
userlist = WaitUser.objects.filter(wait_room=wait_room,is_active=1)
if len(userlist)>=3:
t = {t['url']='redirecturl'}
Group('wait-'+title, channel_layer=message.channel_layer).send({'text': json.dumps(t)})
以下是执行重定向的javascript代码:
$(function() {
var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
var chatsock = new ReconnectingWebSocket(ws_scheme + '://' + window.location.host + window.location.pathname);
chatsock.onmessage = function(message) {
var data = JSON.parse(message.data);
window.location.replace(data.url);
};});