Skip to content

Commit 216cb44

Browse files
committed
update chat room demo
1 parent 2cd63dd commit 216cb44

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

demos/chat_room.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,26 @@
55
from pywebio.output import *
66
from pywebio.session import defer_call, info as session_info, run_async
77

8-
# 最大消息记录保存
98
MAX_MESSAGES_CNT = 10 ** 4
109

11-
chat_msgs = [] # 聊天记录 (name, msg)
12-
online_users = set() # 在线用户
10+
chat_msgs = [] # The chat message history. The item is (name, message content)
11+
online_users = set()
1312

1413

1514
def t(eng, chinese):
1615
"""return English or Chinese text according to the user's browser language"""
1716
return chinese if 'zh' in session_info.user_language else eng
1817

1918

20-
async def refresh_msg(my_name, msg_box):
19+
async def refresh_msg(my_name):
2120
"""send new message to current session"""
2221
global chat_msgs
2322
last_idx = len(chat_msgs)
2423
while True:
2524
await asyncio.sleep(0.5)
2625
for m in chat_msgs[last_idx:]:
2726
if m[0] != my_name: # only refresh message that not sent by current user
28-
msg_box.append(put_markdown('`%s`: %s' % m, sanitize=True))
27+
put_markdown('`%s`: %s' % m, sanitize=True, scope='msg-box')
2928

3029
# remove expired message
3130
if len(chat_msgs) > MAX_MESSAGES_CNT:
@@ -38,26 +37,24 @@ async def main():
3837
"""PyWebIO chat room
3938
4039
You can chat with everyone currently online.
41-
和当前所有在线的人聊天
4240
"""
4341
global chat_msgs
4442

4543
put_markdown(t("## PyWebIO chat room\nWelcome to the chat room, you can chat with all the people currently online. You can open this page in multiple tabs of your browser to simulate a multi-user environment. This application uses less than 90 lines of code, the source code is [here](https://github.com/wang0618/PyWebIO/blob/dev/demos/chat_room.py)", "## PyWebIO聊天室\n欢迎来到聊天室,你可以和当前所有在线的人聊天。你可以在浏览器的多个标签页中打开本页面来测试聊天效果。本应用使用不到90行代码实现,源代码[链接](https://github.com/wang0618/PyWebIO/blob/dev/demos/chat_room.py)"))
4644

47-
msg_box = output()
48-
put_scrollable(msg_box, height=300, keep_bottom=True)
45+
put_scrollable(put_scope('msg-box'), height=300, keep_bottom=True)
4946
nickname = await input(t("Your nickname", "请输入你的昵称"), required=True, validate=lambda n: t('This name is already been used', '昵称已被使用') if n in online_users or n == '📢' else None)
5047

5148
online_users.add(nickname)
5249
chat_msgs.append(('📢', '`%s` joins the room. %s users currently online' % (nickname, len(online_users))))
53-
msg_box.append(put_markdown('`📢`: `%s` join the room. %s users currently online' % (nickname, len(online_users)), sanitize=True))
50+
put_markdown('`📢`: `%s` join the room. %s users currently online' % (nickname, len(online_users)), sanitize=True, scope='msg-box')
5451

5552
@defer_call
5653
def on_close():
5754
online_users.remove(nickname)
5855
chat_msgs.append(('📢', '`%s` leaves the room. %s users currently online' % (nickname, len(online_users))))
5956

60-
refresh_task = run_async(refresh_msg(nickname, msg_box))
57+
refresh_task = run_async(refresh_msg(nickname))
6158

6259
while True:
6360
data = await input_group(t('Send message', '发送消息'), [
@@ -68,7 +65,7 @@ def on_close():
6865
break
6966
if data['cmd'] == t('Multiline Input', '多行输入'):
7067
data['msg'] = '\n' + await textarea('Message content', help_text=t('Message content supports Markdown syntax', '消息内容支持Markdown语法'))
71-
msg_box.append(put_markdown('`%s`: %s' % (nickname, data['msg']), sanitize=True))
68+
put_markdown('`%s`: %s' % (nickname, data['msg']), sanitize=True, scope='msg-box')
7269
chat_msgs.append((nickname, data['msg']))
7370

7471
refresh_task.close()

0 commit comments

Comments
 (0)