Files
mayfly-go/server/internal/msg/msgx/sender/ws.go
2025-07-27 21:02:48 +08:00

41 lines
862 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package sender
import (
"context"
"mayfly-go/internal/msg/msgx"
"mayfly-go/pkg/i18n"
"mayfly-go/pkg/utils/stringx"
"mayfly-go/pkg/ws"
"github.com/spf13/cast"
)
type WsSender struct{}
func (e WsSender) Send(ctx context.Context, channel *msgx.Channel, msg *msgx.Msg) error {
var err error
content := msg.Content
// 存在i18n msgIdcontent则使用msgId翻译
if msgId := msg.TmplExtra.GetInt("msgId"); msgId != 0 {
content = i18n.TC(ctx, i18n.MsgId(msgId))
}
if content != "" {
content, err = stringx.TemplateParse(content, msg.Params)
if err != nil {
return err
}
}
jsonMsg := msg.TmplExtra
jsonMsg["msg"] = content
jsonMsg["title"] = msg.Title
jsonMsg["params"] = msg.Params
for _, receiver := range msg.Receivers {
ws.SendJsonMsg(ws.UserId(receiver.Id), cast.ToString(msg.Params["clientId"]), jsonMsg)
}
return nil
}