我从一个用户的WebSocket连接切换到聊天室,两个人可以互相连接,但在切换时,在接收方法中,现在需要group_send,然后它停止工作,如何修复?
完整追溯
> Exception inside application: No handler for message type
> websocket.group_send File
> "D:\Dev\Web\Chaty\lib\site-packages\channels\sessions.py", line 183,
> in __call__
> return await self.inner(receive, self.send) File "D:\Dev\Web\Chaty\lib\site-packages\channels\middleware.py", line 41,
> in coroutine_call
> await inner_instance(receive, send) File "D:\Dev\Web\Chaty\lib\site-packages\channels\consumer.py", line 59, in
> __call__
> [receive, self.channel_receive], self.dispatch File "D:\Dev\Web\Chaty\lib\site-packages\channels\utils.py", line 52, in
> await_many_dispatch
> await dispatch(result) File "D:\Dev\Web\Chaty\lib\site-packages\channels\consumer.py", line 75, in
> dispatch
> raise ValueError("No handler for message type %s" % message["type"]) No handler for message type websocket.group_send
> WebSocket DISCONNECT /messages/dildo/ [127.0.0.1:58910]
字符串
消费者
import asyncio
import json
from django.contrib.auth import get_user_model
from channels.consumer import AsyncConsumer
from channels.db import database_sync_to_async
from .models import Thread, ChatMessage
class ChatConsumer(AsyncConsumer):
async def websocket_connect(self, event):
print('Connected', event)
other_user = self.scope['url_route']['kwargs']['username']
me = self.scope['user']
thread_obj = await self.get_thread(me, other_user)
chat_room = f"thread_{thread_obj.id}"
self.chat_room = chat_room
await self.channel_layer.group_add(
chat_room,
self.channel_name
)
await self.send({
'type':'websocket.accept'
})
async def websocket_receive(self, event):
print('Recive', event)
front_response = event.get('text', None)
if front_response is not None:
compiled_response_data = json.loads(front_response)
if 'FormData' in compiled_response_data:
pass
author = str(self.scope['user'])
Response_ = {
'Message': compiled_response_data['FormData']['message'],
'Author': author
}
new_event = {
'type':'websocket.send',
'text': json.dumps(Response_)
}
print(new_event)
await self.channel_layer.group_send(
self.chat_room,
new_event
)
async def websocket_disconnect(self, event):
print('Disconnected', event)
@database_sync_to_async
def get_thread(self, user, other_username):
return Thread.objects.get_or_new(user, other_username)[0]
型
我的html模板
{% extends "base.html" %}
{% block content %}
<h3>Thread for {% if user != object.first %}{{ object.first }}{% else %}{{ object.second }}{% endif %}</h3>
<ul id='chat-items'>
{% for chat in object.chatmessage_set.all %}
<li>{{ chat.message }} via {{ chat.user }}</li>
{% endfor %}
</ul>
<form id='form' method='POST'> {% csrf_token %}
{{form }}
<input type="text" name='porn'>
<input type="text" name='pov'>
<button type='submit' class='btn btn-primary'>Submit</button>
</form>
{% endblock %}
{% block script %}
<script>
const from_user = '{{ object.first }}',
to_user = '{{ object.second }}';
// websocket scripts
let loc = window.location,
path_first_part = 'ws://',
path_second_part = loc.host + loc.pathname,
endpoint = '',
FormItself = $('#form'),
FormData = $('#form input'),
DataHolder = {},
ChatItself = $("#chat-items");
if (loc.protocol == 'https:'){
path_first_part = 'wss://'
}else{
path_first_part = 'ws://'
}
endpoint = path_first_part + path_second_part
let socket = new WebSocket(endpoint);
socket.onmessage = function(e){
console.log('Message', e);
let parset_data = JSON.parse(e.data),
Chat_MSG = parset_data.Message,
Chat_AUTHOR = parset_data.Author;
ChatItself.append('<li>' + Chat_MSG + ' <= via => ' + Chat_AUTHOR + '</li>');
};
socket.onopen = function(e){
console.log('Open', e)
FormItself.submit(function(event){
event.preventDefault();
DataHolder['FormData'] = {};
FormData.each(function(key, value){
DataHolder['FormData'][value.name] = value.value;
value.value = '';
})
socket.send(JSON.stringify(DataHolder));
})
console.log(DataHolder)
};
socket.onerror = function(e){
console.log('Error', e)
};
socket.onclose = function(e){
console.log('Close', e)
};
</script>
{% endblock %}
型
看起来你的帖子大部分都是代码;请补充一些细节。
2条答案
按热度按时间2skhul331#
兄弟,很高兴能帮到你,我自己也碰到过一个,只要你建了一个群,那么你就需要向群里发消息,也就是在WebSocket_receive里给
type: 'websocket.send'
放一个回调函数,在那里你已经可以使用向websocket发消息了。字符串
接下来创建chat_message回调函数
型
最后你的消费者会是这样的。
型
7ivaypg92#
简短回答:
检查是否有其他消费者具有相同的组名称,但没有所需的处理程序。每个消费者类都必须有一个唯一的组名,除非您特别希望这样做。
详细回答:
我遇到了类似的问题。我让消费者连接到组“user_1”。当向组“user_1”发送消息时,我得到异常“应用程序内部异常:没有消息类型“new.message”的处理程序,连接已关闭。
字符串
在深入研究代码后,我发现有2个不同的消费者连接到同一个组。
型
当通过
型
两个消费者都接收消息。第一个消费者(我认为这是问题所在)处理了它,没有错误,但第二个消费者没有正确的处理程序。因此,在第二个消费者中发生了前导错误。我为不同的消费者更改了组名,一切都正常工作。
型
发送地不要忘了改群名
型