added channel layer so websockets can talk to each other

This commit is contained in:
iou1name 2018-08-24 22:54:27 -04:00
parent a51db58cbc
commit be94ac7d4f
4 changed files with 41 additions and 8 deletions

View File

@ -4,7 +4,7 @@ By popular demand, I'm building a better anonkun. It doesn't do much right now t
## Requirements ## Requirements
Python 3.6+ Python 3.6+
PostgreSQL 10.4+ PostgreSQL 10.4+
Python packages: `django psycopg2 channels jinja2 argon2-cffi` Python packages: `django psycopg2 channels channels_redis jinja2 argon2-cffi`
## Install ## Install
``` ```

View File

@ -4,6 +4,7 @@ Consumers available for the /quest websocket.
""" """
import json import json
from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer from channels.generic.websocket import WebsocketConsumer
from .events import events from .events import events
@ -21,15 +22,26 @@ class QuestConsumer(WebsocketConsumer):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def connect(self): def connect(self):
self.quest_id = self.scope['url_route']['kwargs']['quest_id']
self.group_name = 'quest_' + str(self.quest_id)
async_to_sync(self.channel_layer.group_add)(
self.group_name,
self.channel_name
)
self.accept() self.accept()
def disconnect(self, close_code): def disconnect(self, close_code):
pass async_to_sync(self.channel_layer.group_discard)(
self.group_name,
self.channel_name
)
def receive(self, text_data): def receive(self, text_data):
""" """
Parses the received data as JSON and dispatches the appropirate Parses the data received from the WebSocket as JSON and
event handler. dispatches the appropriate event handler.
""" """
try: try:
data = json.loads(text_data) data = json.loads(text_data)
@ -42,9 +54,22 @@ class QuestConsumer(WebsocketConsumer):
def send(self, event, data={}): def send(self, event, data={}):
""" """
Overridden method. If a dictionary is provided, it is converted Overridden method. All data provided (preferably as a dictionary)
to JSON before sending it. is parsed into JSON before sending to other consumers in the group.
If a string is provided, it is sent out directly.
""" """
data = json.dumps({'event': event, 'data': data}) data = json.dumps({'event': event, 'data': data})
async_to_sync(self.channel_layer.group_send)(
self.group_name,
{
'type': 'dispatch_send',
'message': data
}
)
def dispatch_send(self, event):
"""
Receives events from other consumers in the group and relays the
data to the WebSocket.
"""
data = event['message']
super().send(text_data=data) super().send(text_data=data)

View File

@ -143,7 +143,7 @@ def text_post(socket, data):
# cleaning # cleaning
post_text = bleach.clean(post_text.strip()) post_text = bleach.clean(post_text.strip())
post_text = text.replace("\n", "<br>") post_text = post_text.replace("\n", "<br>")
# handle image # handle image

View File

@ -147,3 +147,11 @@ PASSWORD_HASHERS = [
] ]
ASGI_APPLICATION = 'titivillus.routing.application' ASGI_APPLICATION = 'titivillus.routing.application'
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('127.0.0.1', 6379)],
},
},
}