Ejemplo n.º 1
0
def index(request):
    # from channels.layers import get_channel_layer
    # from asgiref.sync import async_to_sync
    # channel_layer = get_channel_layer()
    # async_to_sync(channel_layer.group_send)(
    #     "chat_lobby",
    #     {
    #         'type': 'chat.message',
    #         'message': "6666666yyyyy66666666"
    #     }
    # )

    # r = Room.objects.filter(id=46).update(name="33333333") # 如果信号中使用post_save 此更新不会出发信号机制
    r = Room()
    r.name = "xiao"
    r.label = "qq "
    r.save()
    return render(request, 'chat/index.html', {})
Ejemplo n.º 2
0
def generate_room(data):
    user = User.query.filter_by(username=data['username']).first()
    room = Room()
    room.name = data['room_name']

    user.rooms.append(room)

    db.session.commit()

    message = Message(
        user_id=user.id,
        room_id=room.id,
        content=f"{user.username} just created room <b>{room.id}</b>")

    db.session.add(message)

    db.session.commit()

    socketio.emit('room_created_sucessfully', {'room_id': room.id, \
                                                'room_name':room.name \
                                                }, to=request.sid)