Tuesday, June 19, 2018

Re: channels 2 message type handler in different consumer

Here, relevant code:
1 consumer (want it to receive message)
class Cli0Consumer(JsonWebsocketConsumer):
def connect(self):
try:
async_to_sync(self.channel_layer.group_add)("cli0-group", server_name)
except Exception as e:
logging.error(sys.exc_info()[0])

self.accept()

def command_0(self, event):
logger.info(event)
self.send(text_data=event["message"])


class Cli1Consumer(JsonWebsocketConsumer):
def connect(self):
self.accept()
if self._is_authenticated():
try:
async_to_sync(self.channel_layer.group_add)(_group_name("cli1-group", self.channel_name)
#async_to_sync(self.channel_layer.group_add)(_group_name("cli0-group", self.channel_name) - tried with and without subscribing to cli0-group
except Exception as e:
logging.error(sys.exc_info()[0])
else:
self.close(code=4003)

def receive_json(self, content, **kwargs):
group_name = _group_name(self.scope['user'], 'web')
logger.info("received: {}".format(content))

with SomeService("") as monitor:
monitor.watch_message(content=content)

def _is_authenticated(self):
if hasattr(self.scope['headers'], 'auth_error'):
return False
if type(self.scope['user']) is AnonymousUser or not self.scope['user']:
return False
return True


class SomeService(object):
def __enter__(self):
return self


def __init__(self, group_name):
self.cli1_group = group_name
self.cli0_group = group_name.replace('cli1', 'cli0')

def watch_message(self, content):
async_to_sync(channel_layer.group_send)(
self.cligroup_name,
{
"type": "command.status.all",
"message": jsonpickle.encode(msg),
},
)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/53de2993-9697-47bf-ba1b-54c84adea418%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment