Skip to content

Instantly share code, notes, and snippets.

@ashwoods
Created November 8, 2017 15:29
Show Gist options
  • Save ashwoods/b27c56349ce67ad1dac34bdad6bc6530 to your computer and use it in GitHub Desktop.
Save ashwoods/b27c56349ce67ad1dac34bdad6bc6530 to your computer and use it in GitHub Desktop.
channels example
# 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