- "Не удалось подключиться к WebSocket к ws://127.0.0.1:8000/ws/notification/broadcast/". "
- "Сокет чата неожиданно закрылся"
Вот мой код:
Код: Выделить всё
from django.urls import re_path
from . import consumers
websocket_urlpatterns = [
re_path(r'ws/notification/(?P\w+)/$', consumers.NotificationConsumer.as_asgi()),
]
Код: Выделить всё
def notification_view(request):
return render(request,'person/notification.html',{'room_name': "broadcast"})
Код: Выделить всё
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('127.0.0.1', 6379)],
},
},
}
Код: Выделить всё
from notification_app.models import BroadcastNotification
def notifications(request):
allnotifications = BroadcastNotification.objects.all()
return {'notifications': allnotifications}
Код: Выделить всё
class BroadcastNotification(models.Model):
message = models.TextField()
notification_image=models.ImageField(upload_to="notification",default="notification.jpg",blank=True,null=True)
notification_link = models.URLField(max_length=10000, help_text="Add a valid URL",blank=True,null=True)
broadcast_on = models.DateTimeField()
sent = models.BooleanField(default=False)
class Meta:
ordering = ['-broadcast_on']
@receiver(post_save, sender=BroadcastNotification)
def notification_handler(sender, instance, created, **kwargs):
# call group_send function directly to send notificatoions or you can create a dynamic task in celery beat
if created:
schedule, created = CrontabSchedule.objects.get_or_create(hour = instance.broadcast_on.hour, minute = instance.broadcast_on.minute, day_of_month = instance.broadcast_on.day, month_of_year = instance.broadcast_on.month)
task = PeriodicTask.objects.create(crontab=schedule, name="broadcast-notification-"+str(instance.id), task="notifications_app.tasks.broadcast_notification", args=json.dumps((instance.id,)))
Код: Выделить всё
@shared_task(bind = True)
def broadcast_notification(self, data):
print(data)
try:
notification = BroadcastNotification.objects.filter(id = int(data))
if len(notification)>0:
notification = notification.first()
channel_layer = get_channel_layer()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(channel_layer.group_send(
"notification_broadcast",
{
'type': 'send_notification',
'message': json.dumps(notification.message),
}))
notification.sent = True
notification.save()
return 'Done'
else:
self.update_state(
state = 'FAILURE',
meta = {'exe': "Not Found"}
)
raise Ignore()
except:
self.update_state(
state = 'FAILURE',
meta = {
'exe': "Failed"
# 'exc_type': type(ex).__name__,
# 'exc_message': traceback.format_exc().split('\n')
# 'custom': '...'
}
)
raise Ignore()
Код: Выделить всё
{{ room_name|json_script:"room-name" }}
const roomName = JSON.parse(document.getElementById('room-name').textContent);
const notificationSocket = new WebSocket(
'ws://'
+ window.location.host
+ '/ws/notification/'
+ roomName
+ '/'
);
notificationSocket.onmessage = function(e) {
const data = JSON.parse(e.data);
//document.querySelector('#chat-log').value += (data.message + '\n');
console.log(data);
document.getElementById("notifications-dropdown").innerHTML = "[*]" + data + "" + document.getElementById("notifications-dropdown").innerHTML;
document.getElementById("notification-badge").innerHTML = parseInt(document.getElementById("notification-badge").innerHTML) + 1;
};
notificationSocket.onclose = function(e) {
console.error('Chat socket closed unexpectedly');
};
Код: Выделить всё
class NotificationConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.room_name = self.scope['url_route']['kwargs']['room_name']
self.room_group_name = 'notification_%s' % self.room_name
# Join room group
await self.channel_layer.group_add(
self.room_group_name,
self.channel_name
)
await self.accept()
async def disconnect(self, close_code):
# Leave room group
await self.channel_layer.group_discard(
self.room_group_name,
self.channel_name
)
# Receive message from room group
async def send_notification(self, event):
message = json.loads(event['message'])
# Send message to WebSocket
await self.send(text_data=json.dumps(message))
Подробнее здесь: https://stackoverflow.com/questions/792 ... o-channels