- 后端:扩展 SSE 支持 WebRTC 信令消息转发 - 前端:创建 WebRTC 连接管理模块 (webrtc.ts) - 前端:创建 useVoiceCall 组合式函数 - 前端:在对练房间添加语音通话 UI - 集成 Web Speech API 实现语音转文字
This commit is contained in:
@@ -271,44 +271,56 @@ class PracticeRoomService:
|
||||
self,
|
||||
room_id: int,
|
||||
user_id: int,
|
||||
content: str,
|
||||
role_name: Optional[str] = None
|
||||
content: Optional[str],
|
||||
role_name: Optional[str] = None,
|
||||
message_type: Optional[str] = None,
|
||||
extra_data: Optional[dict] = None
|
||||
) -> PracticeRoomMessage:
|
||||
"""
|
||||
发送聊天消息
|
||||
发送聊天消息或信令消息
|
||||
|
||||
Args:
|
||||
room_id: 房间ID
|
||||
user_id: 发送者ID
|
||||
content: 消息内容
|
||||
role_name: 角色名称
|
||||
message_type: 消息类型(默认为 chat)
|
||||
extra_data: 额外数据(用于 WebRTC 信令等)
|
||||
|
||||
Returns:
|
||||
PracticeRoomMessage: 消息对象
|
||||
"""
|
||||
import json
|
||||
|
||||
# 获取当前消息序号
|
||||
sequence = await self._get_next_sequence(room_id)
|
||||
|
||||
# 如果是信令消息,将 extra_data 序列化到 content 中
|
||||
actual_content = content
|
||||
if extra_data and not content:
|
||||
actual_content = json.dumps(extra_data)
|
||||
|
||||
message = PracticeRoomMessage(
|
||||
room_id=room_id,
|
||||
user_id=user_id,
|
||||
message_type=self.MSG_TYPE_CHAT,
|
||||
content=content,
|
||||
message_type=message_type or self.MSG_TYPE_CHAT,
|
||||
content=actual_content,
|
||||
role_name=role_name,
|
||||
sequence=sequence
|
||||
)
|
||||
|
||||
self.db.add(message)
|
||||
|
||||
# 更新房间统计
|
||||
room = await self.get_room_by_id(room_id)
|
||||
if room:
|
||||
room.total_turns += 1
|
||||
user_role = room.get_user_role(user_id)
|
||||
if user_role == "A":
|
||||
room.role_a_turns += 1
|
||||
elif user_role == "B":
|
||||
room.role_b_turns += 1
|
||||
# 只有聊天消息才更新房间统计
|
||||
if (message_type or self.MSG_TYPE_CHAT) == self.MSG_TYPE_CHAT:
|
||||
room = await self.get_room_by_id(room_id)
|
||||
if room:
|
||||
room.total_turns += 1
|
||||
user_role = room.get_user_role(user_id)
|
||||
if user_role == "A":
|
||||
room.role_a_turns += 1
|
||||
elif user_role == "B":
|
||||
room.role_b_turns += 1
|
||||
|
||||
await self.db.commit()
|
||||
await self.db.refresh(message)
|
||||
|
||||
Reference in New Issue
Block a user