コード例 #1
0
    def save(self, *args, **kwargs):
        if not self.name:
            try:
                max_id = max([
                    int(n.split(" ")[-1])
                    for n in BotInstance.objects.all().values_list("name",
                                                                   flat=True)
                ])
            except ValueError:
                max_id = 0

            self.name = "Titandash Instance {id}".format(id=max_id + 1)

        super(BotInstance, self).save(*args, **kwargs)

        # Channels send websocket message.
        channel_layer = get_channel_layer()
        group_name = 'titan_instance'
        instance = self.json()

        # Send message to group.
        async_to_sync(channel_layer.group_send)(group_name, {
            'type': 'saved',
            'instance_id': instance["id"],
            'instance': instance
        })
コード例 #2
0
    def receive(self, text_data):
        data = json.loads(text_data)
        self.commands[data['command']](self, data)

        async_to_sync(self.channel_layer.group_send)(self.room_group_name, {
            'type': 'chat_message',
            'message': data['message']
        })
コード例 #3
0
ファイル: utilities.py プロジェクト: jasonbrent/titandash
 def emit(self, record):
     async_to_sync(self.channel_layer.group_send)(
         self.group_name, {
             'type': 'emitted',
             'instance_id': self.instance.pk,
             'record': {
                 "message": self.format_record(record)
             }
         })
コード例 #4
0
 def connect(self):
     print('got here')
     self.user = self.scope['user']
     self.room_name = self.scope['url_route']['kwargs']['room_name']
     self.room_group_name = 'chat_%s' % self.room_name
     print(self.room_name)
     # Join room group
     # TODO: Find how to limit no of users listening in a channel layer
     async_to_sync(self.channel_layer.group_add)(self.room_group_name,
                                                 self.channel_name)
     self.accept()
コード例 #5
0
    def fetch_messages(self, data):
        room = Room.objects.filter(room_name=self.room_group_name).first()
        if room:
            room_id = room.id
            message = Message.objects.filter(chatroom=room_id).all()
            content = {
                'command': 'messages',
                'messages': self.messagesJson(message)
            }

            async_to_sync(self.send(text_data=json.dumps(content)))
コード例 #6
0
ファイル: prestige.py プロジェクト: jasonbrent/titandash
    def save(self, *args, **kwargs):
        super(Prestige, self).save(*args, **kwargs)

        # Channels and websocket message.
        channel_layer = get_channel_layer()
        group_name = "titan_prestige"
        prestige = self.json()

        async_to_sync(channel_layer.group_send)(group_name, {
            "type": "saved",
            "instance_id": self.instance.pk,
            "prestige": prestige
        })
コード例 #7
0
 def fetch_chat_rooms(self, data):
     # NOTE: Add id to user data in frontend
     user = User.objects.filter(username=data['username']).first()
     id = user.id
     rooms = Room.objects.filter(friend_id=id).all() | Room.objects.filter(
         you_id=id).all()
     # serialize_rooms =
     async_to_sync(
         self.send(text_data=json.dumps({
             'command':
             'add_chatroom',
             'rooms': [RoomSerializer(room).data for room in rooms]
         })))
コード例 #8
0
ファイル: queue.py プロジェクト: jasonbrent/titandash
    def save(self, *args, **kwargs):
        super(Queue, self).save(*args, **kwargs)

        # Channels send websocket message.
        channel_layer = get_channel_layer()
        group_name = "titan_queued"
        queued = self.json()

        async_to_sync(channel_layer.group_send)(
            group_name, {
                "type": "saved",
                'instance_id': self.instance.pk,
                "queued": queued
            }
        )
コード例 #9
0
    def new_message(self, data):
        room = Room.objects.filter(room_name=self.room_group_name).first()
        user = User.objects.filter(id=1).first()
        if not room:
            room_info = {'room_name': self.room_group_name}
            new_room = Room(**room_info)
            new_room.save()
            room = Room.objects.filter(room_name=self.room_group_name).first()
        msg_dict = {'chatroom': room, 'user': user, 'content': data['message']}
        new_msg = Message(**msg_dict)
        new_msg.save()

        async_to_sync(
            self.send(text_data=json.dumps({
                'command':
                'new_message',
                'messages':
                self.messagesJson(Message.objects.filter(id=new_msg.id))[0]
            })))
コード例 #10
0
ファイル: queue.py プロジェクト: jasonbrent/titandash
    def finish(self):
        """
        When a queued function is executed, we can call the finish method to send a signal
        to out WebSocket to let us know it's been removed.
        """
        # Channels send websocket message.
        channel_layer = get_channel_layer()
        group_name = "titan_queued"
        queued = self.json()

        # Send message to group.
        async_to_sync(channel_layer.group_send)(
            group_name, {
                "type": "finished",
                'instance_id': self.instance.pk,
                "queued": queued
            }
        )

        # Delete the queued function after websocket message is sent.
        self.delete()
コード例 #11
0
 def disconnect(self, close_code):
     # Leave room group
     async_to_sync(self.channel_layer.group_discard)(self.room_group_name,
                                                     self.channel_name)
コード例 #12
0
 def send_chat_message(self, message):
     # Send message to room group
     async_to_sync(self.channel_layer.group_send)(self.room_group_name, {
         'type': 'chat_message',
         'message': message
     })