Skip to content

Commit

Permalink
docs: 优化Python连接示例
Browse files Browse the repository at this point in the history
  • Loading branch information
1962247851 committed Apr 1, 2024
1 parent 810a1ce commit 4d954ec
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions connector-examples/python/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import asyncio
Expand Down Expand Up @@ -70,12 +82,12 @@ def on_next(self, value: Payload, is_complete=False):
if msg_type == "DANMU":
msg = msg_dto['msg']
logging.info(
f"{msg_dto['roomId']} 收到弹幕 {str(msg['badgeLevel']) + msg['badgeName'] if msg['badgeLevel'] != 0 else ''} {msg['username']}({str(msg['uid'])}):{msg['content']}"
f"{msg_dto['roomId']} 收到弹幕 {str(msg['badgeLevel']) + str(msg['badgeName']) if msg['badgeLevel'] != 0 else ''} {msg['username']}({str(msg['uid'])}):{msg['content']}"
)
elif msg_type == "GIFT":
msg = msg_dto['msg']
logging.info(
f"{msg_dto['roomId']} 收到礼物 {str(msg['badgeLevel']) + msg['badgeName'] if msg['badgeLevel'] != 0 else ''} {msg['username']}({str(msg['uid'])}) {msg['data']['action'] if msg.get('data') is not None and msg.get('data').get('action') is not None else '赠送'} {msg['giftName']}({str(msg['giftId'])})x{str(msg['giftCount'])}({str(msg['giftPrice'])})"
f"{msg_dto['roomId']} 收到礼物 {str(msg['badgeLevel']) + str(msg['badgeName']) if msg['badgeLevel'] != 0 else ''} {msg['username']}({str(msg['uid'])}) {str(msg['data']['action']) if msg.get('data') is not None and msg.get('data').get('action') is not None else '赠送'} {msg['giftName']}({str(msg['giftId'])})x{str(msg['giftCount'])}({str(msg['giftPrice'])})"
)
else:
logging.info("收到消息 " + json.dumps(msg_dto))
Expand Down

0 comments on commit 4d954ec

Please sign in to comment.