重写数据获取与数据返回结构

This commit is contained in:
junleea 2025-03-22 17:11:42 +08:00
parent 5e17788a34
commit b81ab8412d
3 changed files with 25 additions and 19 deletions

View File

@ -101,7 +101,7 @@ func subscribeAndHandleIMMessages(ws *websocket.Conn, userId int) {
log.Println("Read error:", err) log.Println("Read error:", err)
break break
} }
var data proto.WSMessage var data proto.WSMessageReq
err1 := json.Unmarshal(message, &data) err1 := json.Unmarshal(message, &data)
if err1 != nil { if err1 != nil {
log.Println("Error parsing JSON:", err1) log.Println("Error parsing JSON:", err1)
@ -132,17 +132,11 @@ func subscribeAndHandleIMMessages(ws *websocket.Conn, userId int) {
}() }()
go func() { go func() {
var data proto.WSMessage
//从redis订阅消息 //从redis订阅消息
for m := range ch { for m := range ch {
msg := m.Payload // 获取消息,消息格式为json msg := m.Payload // 获取消息,消息格式为json
if msg != "" { if msg != "" {
data.Msg = msg err2 := ws.WriteMessage(websocket.TextMessage, []byte(msg))
data.Type = "msg"
data.SessionID = int(sessionID)
data.ToID = 0
msg_, _ := json.Marshal(data)
err2 := ws.WriteMessage(websocket.TextMessage, msg_)
if err2 != nil { if err2 != nil {
// 发生错误,删除连接 // 发生错误,删除连接
clientsMux.Lock() clientsMux.Lock()

View File

@ -1,14 +1,16 @@
package proto package proto
import "github.com/ollama/ollama/api"
type AIQueueMessage struct { type AIQueueMessage struct {
Type string `json:"type"` //声明不同消息类型 Type string `json:"type"` //声明不同消息类型
ID uint `json:"id"` //消息id ID uint `json:"id"` //消息id
Msg string `json:"msg"` //原始json消息 Msg api.GenerateResponse `json:"msg"` //原始消息
} }
type WSMessage struct { type WSMessage struct {
Type string `json:"type"` //接收及发送消息类型 Type string `json:"type"` //接收及发送消息类型
Msg string `json:"msg"` //消息内容,只进行转发,不做处理 Msg AIQueueMessage `json:"msg"` //消息内容,只进行转发,不做处理
SessionID int `json:"session_id"` //应用层会话id SessionID int `json:"session_id"` //应用层会话id
ToID int `json:"to_id"` //接收者id ToID int `json:"to_id"` //接收者id
} }
@ -16,3 +18,10 @@ type WSMessage struct {
type ModelParam struct { type ModelParam struct {
Model string `json:"model"` //模型名称 Model string `json:"model"` //模型名称
} }
type WSMessageReq struct {
Type string `json:"type"` //接收及发送消息类型
Msg string `json:"msg"` //消息内容,只进行转发,不做处理
SessionID int `json:"session_id"` //应用层会话id
ToID int `json:"to_id"` //接收者id
}

View File

@ -79,7 +79,7 @@ func CreateGeneralMessageService(fromID, toID, msgType, sessionID int, msg strin
var client *api.Client var client *api.Client
var actxMap map[int][]int var actxMap map[int][]int
func WSReceiveMessageService(userID, sessionID int, channel string, msg proto.WSMessage) (error, uint) { func WSReceiveMessageService(userID, sessionID int, channel string, msg proto.WSMessageReq) (error, uint) {
var resErr error var resErr error
var resID uint var resID uint
//处理消息 //处理消息
@ -101,10 +101,10 @@ func WSReceiveMessageService(userID, sessionID int, channel string, msg proto.WS
ctx := context.Background() ctx := context.Background()
robotMsg := "" robotMsg := ""
var aiMsg proto.AIQueueMessage var aiMsg proto.AIQueueMessage
var data proto.WSMessage
respFunc := func(resp api.GenerateResponse) error { respFunc := func(resp api.GenerateResponse) error {
aiMsg.Type = "ollama" aiMsg.Type = "ollama"
respStr, _ := json.Marshal(resp) aiMsg.Msg = resp
aiMsg.Msg = string(respStr)
robotMsg += resp.Response robotMsg += resp.Response
if resp.Done { //该消息完成 if resp.Done { //该消息完成
actx = resp.Context actx = resp.Context
@ -121,7 +121,10 @@ func WSReceiveMessageService(userID, sessionID int, channel string, msg proto.WS
resID = msgID resID = msgID
} }
//发送消息 //发送消息
aiMsgStr, _ := json.Marshal(aiMsg) data.Msg = aiMsg
data.SessionID = sessionID
data.ToID = userID
aiMsgStr, _ := json.Marshal(data)
worker.Publish(channel, string(aiMsgStr), time.Second*60) worker.Publish(channel, string(aiMsgStr), time.Second*60)
return nil return nil
} }