You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			41 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Python
		
	
			
		
		
	
	
			41 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Python
		
	
| from channels.generic.websocket import WebsocketConsumer 
 | |
| from .models import * 
 | |
| import json
 | |
| from django.template.loader import render_to_string
 | |
| from asgiref.sync import async_to_sync
 | |
| 
 | |
| 
 | |
| 
 | |
| class OsitcomChatRoom(WebsocketConsumer):
 | |
|     def connect(self):
 | |
|         async_to_sync(self.channel_layer.group_add)(
 | |
|             'ositcom_chat', self.channel_name
 | |
|         )
 | |
|         self.accept()
 | |
|         self.send_template()
 | |
| 
 | |
|     def disconnect(self, close_code):
 | |
|         async_to_sync(self.channel_layer.group_discard)(
 | |
|             "ositcom_chat",
 | |
|             self.channel_name
 | |
|         )
 | |
| 
 | |
|     def send_template(self):
 | |
|         event = {
 | |
|             'type': 'send_template_handler',
 | |
|         }
 | |
|         async_to_sync(self.channel_layer.group_send)(
 | |
|             'ositcom_chat', event
 | |
|         )
 | |
| 
 | |
|     def send_template_handler(self, event):
 | |
|         context = {
 | |
| 
 | |
|         }
 | |
|         html = render_to_string("test.html", context=context)
 | |
|         self.send(text_data=json.dumps({
 | |
|             'event_type': 'chat',
 | |
|             'html': html,
 | |
|         }))
 | |
| 
 |