91 lines
2.8 KiB
Go
91 lines
2.8 KiB
Go
package biz
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/go-kratos/kratos/v2/log"
|
|
)
|
|
|
|
// Socket 是Socket模型
|
|
type Socket struct {
|
|
ID string
|
|
Message string
|
|
}
|
|
|
|
// SocketRepo 是Socket repo的接口
|
|
type SocketRepo interface {
|
|
SendMessage(ctx context.Context, to []string, from []string, message []string) error
|
|
IsOnline(ctx context.Context, userID string) (bool, error)
|
|
UpdateUserStatus(ctx context.Context, userID string, roomID string, connected bool) error
|
|
GetOnlineUsers(ctx context.Context, roomID string) ([]string, error)
|
|
CleanupInactiveUsers(ctx context.Context, timeout time.Duration) error
|
|
}
|
|
|
|
// SocketUsecase 是Socket usecase的接口
|
|
type SocketUsecase interface {
|
|
SendMessage(ctx context.Context, to []string, from []string, message []string) error
|
|
IsOnline(ctx context.Context, userID string) (bool, error)
|
|
UpdateUserStatus(ctx context.Context, userID string, roomID string, connected bool) error
|
|
GetOnlineUsers(ctx context.Context, roomID string) ([]string, error)
|
|
CleanupInactiveUsers(ctx context.Context, timeout time.Duration) error
|
|
}
|
|
|
|
// socketUsecase 是Socket usecase的实现
|
|
type socketUsecase struct {
|
|
repo SocketRepo
|
|
log *log.Helper
|
|
}
|
|
|
|
// NewSocketUsecase 创建一个新的Socket usecase
|
|
func NewSocketUsecase(repo SocketRepo, logger log.Logger) SocketUsecase {
|
|
return &socketUsecase{repo: repo, log: log.NewHelper(logger)}
|
|
}
|
|
|
|
// SendMessage 发送消息
|
|
func (su *socketUsecase) SendMessage(ctx context.Context, to []string, from []string, message []string) error {
|
|
// 参数验证
|
|
if len(to) == 0 {
|
|
return errors.New("receiver list is empty")
|
|
}
|
|
if len(message) == 0 {
|
|
return errors.New("message is empty")
|
|
}
|
|
|
|
// 检查接收者是否在线
|
|
for _, receiver := range to {
|
|
online, err := su.repo.IsOnline(ctx, receiver)
|
|
if err != nil {
|
|
su.log.WithContext(ctx).Errorf("check online status failed: %v", err)
|
|
return err
|
|
}
|
|
if !online {
|
|
su.log.WithContext(ctx).Warnf("receiver %s is offline", receiver)
|
|
}
|
|
}
|
|
|
|
// 发送消息
|
|
return su.repo.SendMessage(ctx, to, from, message)
|
|
}
|
|
|
|
// IsOnline 检查用户是否在线
|
|
func (su *socketUsecase) IsOnline(ctx context.Context, userID string) (bool, error) {
|
|
return su.repo.IsOnline(ctx, userID)
|
|
}
|
|
|
|
// UpdateUserStatus 更新用户在线状态
|
|
func (su *socketUsecase) UpdateUserStatus(ctx context.Context, userID string, roomID string, connected bool) error {
|
|
return su.repo.UpdateUserStatus(ctx, userID, roomID, connected)
|
|
}
|
|
|
|
// GetOnlineUsers 获取在线用户列表
|
|
func (su *socketUsecase) GetOnlineUsers(ctx context.Context, roomID string) ([]string, error) {
|
|
return su.repo.GetOnlineUsers(ctx, roomID)
|
|
}
|
|
|
|
// CleanupInactiveUsers 清理不活跃用户
|
|
func (su *socketUsecase) CleanupInactiveUsers(ctx context.Context, timeout time.Duration) error {
|
|
return su.repo.CleanupInactiveUsers(ctx, timeout)
|
|
}
|