Example #1
0
def room(room_id):
    room_online_user_channel = app.config["ROOM_ONLINE_USER_CHANNEL"].format(
        room=room_id)
    room_content_channel = app.config["ROOM_CONTENT_CHANNEL"].format(
        room=room_id)
    room_info_key = app.config["ROOM_INFO_KEY"].format(room=room_id)

    if session["room"] is not None:
        session["room"] = None
        rc.zrem(room_online_user_channel, current_user.username)
    session["room"] = str(room_id)
    rc.zadd(room_online_user_channel, current_user.username, time.time())

    room_info = json.loads(rc.get(room_info_key))

    room_content = reversed(
        rc.zrevrange(room_content_channel, 0, 200, withscores=True))
    room_content_list = []
    for item in room_content:
        room_content_list.append(json.loads(item[0]))

    room_online_users = []
    for user in rc.zrange(room_online_user_channel, 0, -1):
        room_online_users.append(user)

    return render_template("room.html",
                           room_id=room_id,
                           room_info=room_info,
                           users=room_online_users,
                           user_name=current_user.username,
                           messages=room_content_list)
Example #2
0
def room(room_id):
    room_online_user_channel = app.config["ROOM_ONLINE_USER_CHANNEL"].format(room=room_id)
    room_content_channel = app.config["ROOM_CONTENT_CHANNEL"].format(room=room_id)
    room_info_key = app.config["ROOM_INFO_KEY"].format(room=room_id)

    if session["room"] is not None:
        session["room"] = None
        rc.zrem(room_online_user_channel, current_user.username)
    session["room"] = str(room_id)
    rc.zadd(room_online_user_channel, current_user.username, time.time())
    
    room_info = json.loads(rc.get(room_info_key))

    room_content = reversed(rc.zrevrange(room_content_channel, 0, 200, withscores=True))
    room_content_list = []
    for item in room_content:
        room_content_list.append(json.loads(item[0]))

    room_online_users =[]
    for user in rc.zrange(room_online_user_channel, 0, -1):
        room_online_users.append(user)

    return render_template("room.html", 
                           room_id=room_id, 
                           room_info=room_info,
                           users=room_online_users, 
                           user_name=current_user.username,
                           messages=room_content_list)
Example #3
0
def on_new_message(message):
    data = {"user": current_user.username,
            "content": excape_text(message["data"]),
            "created": datetime.datetime.now().strftime("%a %b %d %H:%M:%S %Y"),
            "room_id": session["room"],
            "id":rc.incr(app.config["ROOM_CONTENT_INCR_KEY"])
    }
    rc.zadd(app.config["ROOM_ONLINE_USER_CHANNEL"].format(room=session["room"]), json.dumps(data), time.time())
    emit("new_message", {
        "user": current_user.username,
        "time": datetime.datetime.now().strftime("%a %b %d %H:%M:%S %Y"),
        "data": excape_text(message["data"])
    }, room=session["room"])
Example #4
0
def on_new_message(message):
    data = {
        "user": current_user.username,
        "content": excape_text(message["data"]),
        "created": datetime.datetime.now().strftime("%a %b %d %H:%M:%S %Y"),
        "room_id": session["room"],
        "id": rc.incr(app.config["ROOM_CONTENT_INCR_KEY"])
    }
    rc.zadd(
        app.config["ROOM_ONLINE_USER_CHANNEL"].format(room=session["room"]),
        json.dumps(data), time.time())
    emit("new_message", {
        "user": current_user.username,
        "time": datetime.datetime.now().strftime("%a %b %d %H:%M:%S %Y"),
        "data": excape_text(message["data"])
    },
         room=session["room"])