Created
November 8, 2017 15:29
-
-
Save ashwoods/b27c56349ce67ad1dac34bdad6bc6530 to your computer and use it in GitHub Desktop.
channels example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In consumers.py | |
import random | |
import json | |
from channels import Group | |
from raven.contrib.django.models import get_client | |
client = get_client() | |
# Connected to websocket.connect | |
def ws_add(message): | |
message.reply_channel.send({"accept": True}) | |
Group("test").add(message.reply_channel) | |
# Connected to websocket.disconnect | |
def ws_disconnect(message): | |
Group("test").discard(message.reply_channel) | |
@client.capture_exceptions | |
def ws_message(message): | |
num = random.randint(1, 10) | |
if num >= 5: | |
response = json.dumps({'answer': num}) | |
else: | |
response = json.dumps({'answer': 1/0}) | |
Group("test").send({'text': response}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment