频道不向合作伙伴浏览器发送消息
Channel not sending message to partners browser
我正在尝试将聊天数据发送到我的浏览器。 websocket 正在向消息所有者的浏览器发送数据。但问题是消息没有从其他用户的浏览器更新。
class ChatConsumer(WebsocketConsumer):
def connect(self):
if(not self.scope['user'] is User):
my_id = self.scope['user']
self.me = User.objects.get(id=my_id)
else:
self.me = self.scope['user']
others_id = self.scope['url_route']['kwargs']['user_id']
other_user = User.objects.get(id=others_id)
self.thread_obj = Thread.objects.get_or_create_personal_thread(
self.me, other_user)
self.room_name = f'presonal_thread_{self.thread_obj.id}'
self.channel_layer.group_add(
self.room_name,
self.channel_name)
self.accept()
print(f'[{self.channel_name}] - {self.me.username} You are connected')
def fetch_messages(self, data):
messages = Message.objects.filter(thread=self.thread_obj)
content = {
'command': 'messages',
'messages': self.messages_to_json(messages)
}
self.send_message(content)
def new_message(self, data):
message = Message.objects.create(sender=self.me,
thread=self.thread_obj,
message=data['message'])
content = {
'command': 'new_message',
'message': self.message_to_json(message)
}
return self.chat_message(content)
def messages_to_json(self, messages):
result = []
for message in messages:
result.append(self.message_to_json(message))
return result
def message_to_json(self, message):
return {
'id': message.id,
'sender_id': message.sender.id,
'sender': message.sender.username,
'content': message.message,
'timestamp': str(message.posted_on)
}
commands = {
'fetch_messages': fetch_messages,
'new_message': new_message
}
def disconnect(self, close_code):
self.channel_layer.group_discard(
self.room_name,
self.channel_name
)
def receive(self, text_data):
data = json.loads(text_data)
self.commands[data['command']](self, data)
def chat_message(self, message):
print(message)
async_to_sync(self.channel_layer.group_send)(
self.room_name,
{
'type': 'chat_message',
'message': message
}
)
self.send(text_data=json.dumps(message))
def send_message(self, message):
self.send(text_data=json.dumps(message))
当我从浏览器向通道套接字发送聊天消息时,它只更新到我的浏览器。但不会用新消息更新其他用户的浏览器。
N.B: 我是 django 频道的新手。
您可以将“发送到群组”移动到接收方法。您从前端发送的消息应该直接进入接收方法。
def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json['message']
# Send message to room group
async_to_sync(self.channel_layer.group_send)(
self.room_group_name,
{
'type': 'chat_message',
'message': message
}
)
我正在尝试将聊天数据发送到我的浏览器。 websocket 正在向消息所有者的浏览器发送数据。但问题是消息没有从其他用户的浏览器更新。
class ChatConsumer(WebsocketConsumer):
def connect(self):
if(not self.scope['user'] is User):
my_id = self.scope['user']
self.me = User.objects.get(id=my_id)
else:
self.me = self.scope['user']
others_id = self.scope['url_route']['kwargs']['user_id']
other_user = User.objects.get(id=others_id)
self.thread_obj = Thread.objects.get_or_create_personal_thread(
self.me, other_user)
self.room_name = f'presonal_thread_{self.thread_obj.id}'
self.channel_layer.group_add(
self.room_name,
self.channel_name)
self.accept()
print(f'[{self.channel_name}] - {self.me.username} You are connected')
def fetch_messages(self, data):
messages = Message.objects.filter(thread=self.thread_obj)
content = {
'command': 'messages',
'messages': self.messages_to_json(messages)
}
self.send_message(content)
def new_message(self, data):
message = Message.objects.create(sender=self.me,
thread=self.thread_obj,
message=data['message'])
content = {
'command': 'new_message',
'message': self.message_to_json(message)
}
return self.chat_message(content)
def messages_to_json(self, messages):
result = []
for message in messages:
result.append(self.message_to_json(message))
return result
def message_to_json(self, message):
return {
'id': message.id,
'sender_id': message.sender.id,
'sender': message.sender.username,
'content': message.message,
'timestamp': str(message.posted_on)
}
commands = {
'fetch_messages': fetch_messages,
'new_message': new_message
}
def disconnect(self, close_code):
self.channel_layer.group_discard(
self.room_name,
self.channel_name
)
def receive(self, text_data):
data = json.loads(text_data)
self.commands[data['command']](self, data)
def chat_message(self, message):
print(message)
async_to_sync(self.channel_layer.group_send)(
self.room_name,
{
'type': 'chat_message',
'message': message
}
)
self.send(text_data=json.dumps(message))
def send_message(self, message):
self.send(text_data=json.dumps(message))
当我从浏览器向通道套接字发送聊天消息时,它只更新到我的浏览器。但不会用新消息更新其他用户的浏览器。
N.B: 我是 django 频道的新手。
您可以将“发送到群组”移动到接收方法。您从前端发送的消息应该直接进入接收方法。
def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json['message']
# Send message to room group
async_to_sync(self.channel_layer.group_send)(
self.room_group_name,
{
'type': 'chat_message',
'message': message
}
)