Compare commits
1 Commits
4d98a66fd9
...
8513cdd86e
Author | SHA1 | Date | |
---|---|---|---|
8513cdd86e |
|
@ -4,7 +4,8 @@ By popular demand, I'm building a better anonkun. It doesn't do much right now t
|
|||
## Requirements
|
||||
Python 3.6+
|
||||
PostgreSQL 10.4+
|
||||
Python packages: `django psycopg2 channels jinja2 argon2-cffi`
|
||||
Redis 4.0.10+
|
||||
Python packages: `django psycopg2 channels channels_redis jinja2 argon2-cffi`
|
||||
|
||||
## Install
|
||||
```
|
||||
|
|
|
@ -4,6 +4,7 @@ Consumers available for the /quest websocket.
|
|||
"""
|
||||
import json
|
||||
|
||||
from asgiref.sync import async_to_sync
|
||||
from channels.generic.websocket import WebsocketConsumer
|
||||
|
||||
from .events import events
|
||||
|
@ -21,15 +22,26 @@ class QuestConsumer(WebsocketConsumer):
|
|||
super().__init__(*args, **kwargs)
|
||||
|
||||
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()
|
||||
|
||||
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):
|
||||
"""
|
||||
Parses the received data as JSON and dispatches the appropirate
|
||||
event handler.
|
||||
Parses the data received from the WebSocket as JSON and
|
||||
dispatches the appropriate event handler.
|
||||
"""
|
||||
try:
|
||||
data = json.loads(text_data)
|
||||
|
@ -42,9 +54,22 @@ class QuestConsumer(WebsocketConsumer):
|
|||
|
||||
def send(self, event, data={}):
|
||||
"""
|
||||
Overridden method. If a dictionary is provided, it is converted
|
||||
to JSON before sending it.
|
||||
If a string is provided, it is sent out directly.
|
||||
Overridden method. All data provided (preferably as a dictionary)
|
||||
is parsed into JSON before sending to other consumers in the group.
|
||||
"""
|
||||
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)
|
||||
|
|
|
@ -143,7 +143,7 @@ def text_post(socket, data):
|
|||
|
||||
# cleaning
|
||||
post_text = bleach.clean(post_text.strip())
|
||||
post_text = text.replace("\n", "<br>")
|
||||
post_text = post_text.replace("\n", "<br>")
|
||||
|
||||
# handle image
|
||||
|
||||
|
|
|
@ -147,3 +147,11 @@ PASSWORD_HASHERS = [
|
|||
]
|
||||
|
||||
ASGI_APPLICATION = 'titivillus.routing.application'
|
||||
CHANNEL_LAYERS = {
|
||||
'default': {
|
||||
'BACKEND': 'channels_redis.core.RedisChannelLayer',
|
||||
'CONFIG': {
|
||||
"hosts": [('127.0.0.1', 6379)],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user