本文整理汇总了Python中channels.Group.add方法的典型用法代码示例。如果您正苦于以下问题:Python Group.add方法的具体用法?Python Group.add怎么用?Python Group.add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类channels.Group
的用法示例。
在下文中一共展示了Group.add方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: connect_wait_page
# 需要导入模块: from channels import Group [as 别名]
# 或者: from channels.Group import add [as 别名]
def connect_wait_page(message, params):
session_pk, page_index, model_name, model_pk = params.split(',')
session_pk = int(session_pk)
page_index = int(page_index)
model_pk = int(model_pk)
group_name = channels_wait_page_group_name(
session_pk, page_index, model_name, model_pk
)
group = Group(group_name)
group.add(message.reply_channel)
# in case message was sent before this web socket connects
if model_name == 'group':
ready = CompletedGroupWaitPage.objects.filter(
page_index=page_index,
group_pk=model_pk,
session_pk=session_pk,
after_all_players_arrive_run=True).exists()
else: # subsession
ready = CompletedSubsessionWaitPage.objects.filter(
page_index=page_index,
session_pk=session_pk,
after_all_players_arrive_run=True).exists()
if ready:
message.reply_channel.send(
{'text': json.dumps(
{'status': 'ready'})})
示例2: ws_connect
# 需要导入模块: from channels import Group [as 别名]
# 或者: from channels.Group import add [as 别名]
def ws_connect(message):
"""
Channels connection setup.
Register the current client on the related Group according to the language
"""
prefix, language = message['path'].strip('/').split('/')
gr = Group('knocker-{0}'.format(language))
gr.add(message.reply_channel)
message.channel_session['knocker'] = language
示例3: sign_in
# 需要导入模块: from channels import Group [as 别名]
# 或者: from channels.Group import add [as 别名]
def sign_in(self, request, data, *args, **kwargs):
serializer = IsAuthenticatedSerializer(data=data)
serializer.is_valid(raise_exception=True)
band_id = kwargs.get("band_id")
user = self.check_chat_perms(serializer, band_id)
if user is not None:
request.channel_session["user"] = user.id
group = Group(self.CHAT_GROUP_TEMPLATE % band_id)
group.add(request.reply_channel)
messages = Message.objects.select_related("author").filter(band_id=band_id)[: self.MESSAGES_COUNT][::-1]
self.route_send(request.reply_channel, MessagesSerializer({"messages": messages}).data)
else:
raise PermissionDenied
示例4: connect_auto_advance
# 需要导入模块: from channels import Group [as 别名]
# 或者: from channels.Group import add [as 别名]
def connect_auto_advance(message, params):
participant_code, page_index = params.split(',')
page_index = int(page_index)
group = Group('auto-advance-{}'.format(participant_code))
group.add(message.reply_channel)
# in case message was sent before this web socket connects
participant = Participant.objects.get(code=participant_code)
if participant._index_in_pages > page_index:
message.reply_channel.send(
{'text': json.dumps(
{'new_index_in_pages': participant._index_in_pages})}
)
示例5: connect_wait_for_session
# 需要导入模块: from channels import Group [as 别名]
# 或者: from channels.Group import add [as 别名]
def connect_wait_for_session(message, pre_create_id):
group = Group(channels_create_session_group_name(pre_create_id))
group.add(message.reply_channel)
# in case message was sent before this web socket connects
if Session.objects.filter(_pre_create_id=pre_create_id):
group.send(
{'text': json.dumps(
{'status': 'ready'})}
)
elif FailedSessionCreation.objects.filter(
pre_create_id=pre_create_id
).exists():
group.send(
{'text': json.dumps(
{'error': 'Failed to create session. Check the server logs.'})}
)
示例6: connect_wait_for_session
# 需要导入模块: from channels import Group [as 别名]
# 或者: from channels.Group import add [as 别名]
def connect_wait_for_session(message, pre_create_id):
group = Group(channels_create_session_group_name(pre_create_id))
group.add(message.reply_channel)
# in case message was sent before this web socket connects
if Session.objects.filter(_pre_create_id=pre_create_id, ready=True):
group.send(
{'text': json.dumps(
{'status': 'ready'})}
)
else:
failure = FailedSessionCreation.objects.filter(
pre_create_id=pre_create_id
).first()
if failure:
group.send(
{'text': json.dumps(
{'error': failure.message,
'traceback': failure.traceback})}
)
示例7: connect_auto_advance
# 需要导入模块: from channels import Group [as 别名]
# 或者: from channels.Group import add [as 别名]
def connect_auto_advance(message, params):
participant_code, page_index = params.split(',')
page_index = int(page_index)
group = Group('auto-advance-{}'.format(participant_code))
group.add(message.reply_channel)
# in case message was sent before this web socket connects
try:
participant = Participant.objects.get(code=participant_code)
except Participant.DoesNotExist:
message.reply_channel.send(
{'text': json.dumps(
# doesn't get shown because not yet localized
{'error': 'Participant not found in database.'})})
return
if participant._index_in_pages > page_index:
message.reply_channel.send(
{'text': json.dumps(
{'new_index_in_pages': participant._index_in_pages})})
示例8: connect_wait_page
# 需要导入模块: from channels import Group [as 别名]
# 或者: from channels.Group import add [as 别名]
def connect_wait_page(message, params):
app_label, page_index, model_name, model_pk = params.split(',')
page_index = int(page_index)
model_pk = int(model_pk)
group_name = channels_wait_page_group_name(
app_label, page_index, model_name, model_pk
)
group = Group(group_name)
group.add(message.reply_channel)
# in case message was sent before this web socket connects
# fixme: app name or app label?
models_module = common_internal.get_models_module(app_label)
GroupOrSubsession = {
'subsession': getattr(models_module, 'Subsession'),
'group': getattr(models_module, 'Group')
}[model_name]
group_or_subsession = GroupOrSubsession.objects.get(pk=model_pk)
participants_for_this_page = set(
p.participant for p in group_or_subsession.player_set.all()
)
unvisited = set(
p for p in participants_for_this_page if
p._index_in_pages < page_index
)
if not unvisited:
message.reply_channel.send(
{'text': json.dumps(
{'status': 'ready'})})
示例9: connect_auto_advance
# 需要导入模块: from channels import Group [as 别名]
# 或者: from channels.Group import add [as 别名]
def connect_auto_advance(message, params):
participant_code, page_index = params.split(',')
page_index = int(page_index)
group = Group('auto-advance-{}'.format(participant_code))
group.add(message.reply_channel)
# in case message was sent before this web socket connects
result = Participant.objects.filter(
code=participant_code).values_list(
'_index_in_pages', flat=True)
try:
page_should_be_on = result[0]
except IndexError:
message.reply_channel.send(
{'text': json.dumps(
# doesn't get shown because not yet localized
{'error': 'Participant not found in database.'})})
return
if page_should_be_on > page_index:
message.reply_channel.send(
{'text': json.dumps(
{'auto_advanced': True})})
示例10: chat_connect
# 需要导入模块: from channels import Group [as 别名]
# 或者: from channels.Group import add [as 别名]
def chat_connect(message):
room = message.content['path'].strip('/')
message.channel_session['room'] = room
group = Group('chat-%s' % room)
group.add(message.reply_channel)
示例11: ws_connect
# 需要导入模块: from channels import Group [as 别名]
# 或者: from channels.Group import add [as 别名]
def ws_connect(message, room):
g = Group('chat:{}'.format(room))
data = {'text': 'A new user connected', 'type': 'log'}
g.send({'text': json.dumps(data)})
g.add(message.reply_channel)