In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to achieve private chat in an online chat room with Python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to achieve private chat in an online chat room with Python".
Realization idea
For private conversation, I think there are two points that need to be realized.
Private chat list update
Everyone needs to have a list of private conversations and quasi-real-time updates so that the person can know who is going to talk to him in private and who he or she is talking to.
Chat room in private
For private chat rooms, it is possible to reuse group chat rooms, but there are only two people in this chat room. At the same time, for message delivery, the functions implemented in group chat can also be reused.
Front-end layout
Well, now that we have the idea, let's start the layout first. This point for a semi-disabled front end, too Nan Nan, has been engaged for a long time, only the whole layout, slightly ugly, but there is no way.
For me, who can't adjust the style, this can make some simple changes to the original CSS code.
Add a private chat next to the original list of all users
All private chats, no private conversations for the time being.
Then adjust the width of the control so that the new div is displayed next to the chat box instead of below
.chat _ left {padding:20px; width:-moz-calc (100%-440px);} .chat_right {width:199px;} .chat {width:199px;}
All right, that's all the layout of the page has to say. If you say too much, you will cry.
Chat room in private
Now to start writing back-end logic, first we need to have a chat room for private chat, then let's modify the create_room function and create a private chat.
Main.route ('/ createroom/', methods= ["GET", 'POST']) @ login_requireddef create_room (chatwith=None): if chatwith: rname = chatwith if r.exists ("pchat-" + rname +' -'+ current_user.username) is False: r.zadd ("pchat-" + rname +'-'+ current_user.username, current_user.username, 1) r.zadd ("pchat-" + rname +'-'+ current_user.username, rname, 2).
The current function can receive a parameter of chatwith. If the parameter is not None, pchat data is created in redis, that is, a private chat room.
Next, create a private chat page view function, which can be refined later to require certain permissions to initiate a private chat.
@ main.route ('/ privatechat/', methods= ['GET',' POST']) @ login_requireddef private_chat (): add private chat permission user_right = True if user_right: uname = request.args.get ('to', ") create_room (chatwith=uname) ulist = r.zrange (" pchat- "+ uname +' -'+ current_user.username, 0,-1) messages = r.zrange (" pmsg- "+ uname +'-'+ current_user.username, 0) -1, withscores=True) msg_list= [] for i in messages: msg_list.append ([json.loads (I [0]), time.strftime ("% Y/%m/%d% p%H:%M:%S", time.localtime (I [1])]) return render_template ('privatechat.html', rname=uname, user_list=ulist, msg_list=msg_list) else: pass
The following also needs a function that returns a list of private chats.
# get personal chat information @ main.route ('/ api/pchat/', methods= ['GET',' POST']) def pchat_info (user): pchat = r.keys (pattern='pchat-*') pchatlist = [] for i in pchat: i_str = str (I) user1 = i_str.split ('-', 2) [1] if user in i_str: pchatlist.append ({user1: i_str}) html = [] for i in pchatlist: html.append (f')
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.