|
|
|
@ -638,33 +638,36 @@ def handle_send_message(data):
|
|
|
|
|
if not room_id or not message_text:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 获取用户和房间信息
|
|
|
|
|
user = User.query.get(user_id)
|
|
|
|
|
room = ChatRoom.query.get(room_id)
|
|
|
|
|
|
|
|
|
|
if not user or not room:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 保存消息到数据库
|
|
|
|
|
message = ChatMessage(
|
|
|
|
|
room_id=room_id,
|
|
|
|
|
user_id=user_id,
|
|
|
|
|
message=message_text,
|
|
|
|
|
message_type='text'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
db.session.add(message)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
# 广播消息给房间内的所有用户
|
|
|
|
|
emit('new_message', {
|
|
|
|
|
'id': message.id,
|
|
|
|
|
'user_id': user_id,
|
|
|
|
|
'username': user.username,
|
|
|
|
|
'message': message_text,
|
|
|
|
|
'message_type': 'text',
|
|
|
|
|
'created_at': message.created_at.isoformat()
|
|
|
|
|
}, room=str(room_id))
|
|
|
|
|
try:
|
|
|
|
|
with app.app_context():
|
|
|
|
|
user = User.query.get(user_id)
|
|
|
|
|
room = ChatRoom.query.get(room_id)
|
|
|
|
|
|
|
|
|
|
if not user or not room:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
message = ChatMessage(
|
|
|
|
|
room_id=room_id,
|
|
|
|
|
user_id=user_id,
|
|
|
|
|
message=message_text,
|
|
|
|
|
message_type='text'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
db.session.add(message)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
emit('new_message', {
|
|
|
|
|
'id': message.id,
|
|
|
|
|
'user_id': user_id,
|
|
|
|
|
'username': user.username,
|
|
|
|
|
'message': message_text,
|
|
|
|
|
'message_type': 'text',
|
|
|
|
|
'created_at': message.created_at.isoformat()
|
|
|
|
|
}, room=str(room_id))
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"处理文本消息时出错: {e}")
|
|
|
|
|
db.session.rollback()
|
|
|
|
|
|
|
|
|
|
@socketio.on('send_audio_message')
|
|
|
|
|
def handle_send_audio_message(data):
|
|
|
|
@ -704,40 +707,38 @@ def handle_send_audio_message(data):
|
|
|
|
|
logger.info(f"处理语音消息: 用户ID={user_id}, 房间ID={room_id}, 音频时长={audio_duration}秒, MIME类型={audio_mime_type}, 数据长度={len(audio_data)}")
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# 获取用户和房间信息
|
|
|
|
|
user = User.query.get(user_id)
|
|
|
|
|
room = ChatRoom.query.get(room_id)
|
|
|
|
|
|
|
|
|
|
if not user or not room:
|
|
|
|
|
logger.error(f"用户或房间不存在: user_id={user_id}, room_id={room_id}")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 保存消息到数据库
|
|
|
|
|
message = ChatMessage(
|
|
|
|
|
room_id=room_id,
|
|
|
|
|
user_id=user_id,
|
|
|
|
|
message=audio_data, # 存储Base64编码的音频数据
|
|
|
|
|
message_type='audio',
|
|
|
|
|
audio_duration=audio_duration
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
db.session.add(message)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
logger.info(f"语音消息已保存到数据库: ID={message.id}")
|
|
|
|
|
|
|
|
|
|
# 广播消息给房间内的所有用户
|
|
|
|
|
emit('new_message', {
|
|
|
|
|
'id': message.id,
|
|
|
|
|
'user_id': user_id,
|
|
|
|
|
'username': user.username,
|
|
|
|
|
'message': audio_data,
|
|
|
|
|
'message_type': 'audio',
|
|
|
|
|
'audio_duration': audio_duration,
|
|
|
|
|
'audio_mime_type': audio_mime_type,
|
|
|
|
|
'created_at': message.created_at.isoformat()
|
|
|
|
|
}, room=str(room_id))
|
|
|
|
|
logger.info(f"语音消息已广播到房间: {room_id}")
|
|
|
|
|
|
|
|
|
|
with app.app_context():
|
|
|
|
|
user = User.query.get(user_id)
|
|
|
|
|
room = ChatRoom.query.get(room_id)
|
|
|
|
|
|
|
|
|
|
if not user or not room:
|
|
|
|
|
logger.error(f"用户或房间不存在: user_id={user_id}, room_id={room_id}")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
message = ChatMessage(
|
|
|
|
|
room_id=room_id,
|
|
|
|
|
user_id=user_id,
|
|
|
|
|
message=audio_data,
|
|
|
|
|
message_type='audio',
|
|
|
|
|
audio_duration=audio_duration
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
db.session.add(message)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
logger.info(f"语音消息已保存到数据库: ID={message.id}")
|
|
|
|
|
|
|
|
|
|
emit('new_message', {
|
|
|
|
|
'id': message.id,
|
|
|
|
|
'user_id': user_id,
|
|
|
|
|
'username': user.username,
|
|
|
|
|
'message': audio_data,
|
|
|
|
|
'message_type': 'audio',
|
|
|
|
|
'audio_duration': audio_duration,
|
|
|
|
|
'audio_mime_type': audio_mime_type,
|
|
|
|
|
'created_at': message.created_at.isoformat()
|
|
|
|
|
}, room=str(room_id))
|
|
|
|
|
logger.info(f"语音消息已广播到房间: {room_id}")
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"处理语音消息时出错: {e}")
|
|
|
|
|
db.session.rollback()
|
|
|
|
|