1 Star 0 Fork 0

h79 / goim

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
user.go 5.37 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2024-01-05 18:57 . 接口,可以获取连接情况
package im
import (
"fmt"
"gitee.com/h79/goutils/common/option"
"strings"
)
type Copy struct {
Begin func(count int) interface{}
Process func(be interface{}, index int, data interface{})
}
type Status struct {
Session
LoginTime int64 `json:"-"`
TimeAt int64 `json:"timeAt"`
Status int32 `json:"status"`
Desc string `json:"desc"`
TraceId string `json:"-"`
Addr string `json:"-"`
RemoteAddr string `json:"-"`
}
func (s *Status) String() string {
return fmt.Sprintf("AppId: %s, Plat: %d, UserId: %d, Account: %s, TimeAt: %d, Status: %d, Desc: %s",
s.AppId, s.Plat, s.UserId, s.Account, s.TimeAt, s.Status, s.Desc)
}
const (
StatusOffline = 1
StatusOnline = 2
StatusHeartbeat = 3
)
const (
IgnoreStatusBroadcast = 1
)
var StatusDesc = map[int32]string{
StatusOffline: "离开",
StatusOnline: "在线",
}
type App struct {
AppId string `form:"appId" binding:"required" json:"appId" yaml:"appId"`
Plat int32 `form:"plat" binding:"-" json:"plat" yaml:"plat"`
}
type Session struct {
AppId string `form:"appId" binding:"required" json:"appId" yaml:"appId" url:"appId"`
Account string `form:"account" json:"account" yaml:"account" url:"account"`
UserId uint64 `form:"userId" json:"userId,omitempty" yaml:"userId,omitempty" url:"userId,omitempty"`
ClientId uint64 `form:"clientId" json:"clientId,omitempty" yaml:"clientId,omitempty" url:"clientId,omitempty"`
Plat int32 `form:"plat" json:"plat" yaml:"plat" url:"plat"`
Ignore int32 `form:"ignore" json:"ignore,omitempty" yaml:"ignore,omitempty" url:"ignore,omitempty"`
}
func (s *Session) String() string {
return fmt.Sprintf("AppId: %s, Plat: %d, UserId: %d, Account: %s", s.AppId, s.Plat, s.UserId, s.Account)
}
func (s *Session) IgnoreBit(f int32) bool {
return s.Ignore&f == f
}
func (s *Session) EqualAppId(session *Session) bool {
if session.AppId == "" {
return true
}
return strings.EqualFold(session.AppId, s.AppId)
}
func (s *Session) EqualPlat(session *Session) bool {
if session.Plat < 0 {
return true
}
return session.Plat == s.Plat
}
func (s *Session) EqualUserId(c *Session) bool {
return s.UserId > 0 && s.UserId == c.UserId
}
func (s *Session) EqualAccount(c *Session) bool {
return s.Account != "" && s.Account == c.Account
}
func (s *Session) Equal(c *Session) bool {
return (s.EqualAccount(c) || s.EqualUserId(c)) && s.EqualAppId(c) && s.EqualPlat(c)
}
func (s *Session) IsValid() bool {
return (s.UserId > 0 || s.Account != "") && s.AppId != ""
}
// UserHeartbeatTimeout 3分钟
const UserHeartbeatTimeout = 180
type Online struct {
ID string `json:"id"`
AppId string `json:"appId"`
ClientIp string `json:"clientIp"` // 客户端Ip
ClientPort string `json:"clientPort"` // 客户端端口
RemoteIp string `json:"remoteIp"` //用户远程IP
Host string `json:"host"` // 用户所在的服务器ip
Account string `json:"account"`
UserId uint64 `json:"userId"`
Port int `json:"port"` // 用户所在的服务器端口
Plat int32 `json:"plat"`
LoginTime int64 `json:"loginTime"` // 用户上次登录时间
HeartbeatTime int64 `json:"heartbeatTime"` // 用户上次心跳时间
LogOutTime int64 `json:"logOutTime"` // 用户退出登录的时间
IsOffline bool `json:"isOffline"` // 是否离线
}
func (u *Online) ToSession() Session {
return Session{AppId: u.AppId, Plat: u.Plat, UserId: u.UserId, Account: u.Account}
}
func (u *Online) Heartbeat(currentTime int64) {
u.HeartbeatTime = currentTime
u.IsOffline = false
}
func (u *Online) LogIn(currentTime int64) {
u.LoginTime = currentTime
u.Heartbeat(currentTime)
}
func (u *Online) LogOut(currentTime int64) {
u.LogOutTime = currentTime
u.IsOffline = true
}
func (u *Online) IsOnline(heartCheck func(heartbeat int64) bool) bool {
if u.IsOffline {
return false
}
if heartCheck != nil {
return heartCheck(u.HeartbeatTime)
}
return true
}
// IsLocal 用户是否在本台机器上
func (u *Online) IsLocal(localIp string, localPort int) bool {
return u.Host == localIp && u.Port == localPort
}
func (u *Online) String() string {
return fmt.Sprintf("{AppId: '%s' UserId: '%d' Plat: '%d' Host: '%s' Port: '%d' IsOffline: '%v'}",
u.AppId, u.UserId, u.Plat, u.Host, u.Port, u.IsOffline)
}
type OnLineList struct {
Count int `json:"count"`
List []*Online `json:"list,omitempty"`
}
type User interface {
SetSupportApp(apps []App)
NextSupportApp(fn func(app App) bool)
ExistSupportApp(app App) bool
HeartbeatTimeout() int64
GetUserKey(session *Session) string
GetUserOnline(session *Session, opt ...option.Option) (*Online, error)
// SetUserOnline 写到redis中
SetUserOnline(session *Session, online *Online) error
RemoveUserOnline(session *Session) error
IsUserOnline(session *Session) (bool, error)
GetAllOnline(cc *Copy, session *Session, opt ...option.Option) error
//UserStatusChanged 通知基它服务有变化
UserStatusChanged(status *Status, opt ...option.Option)
}
type ConnectInfo struct {
Session
Version string `form:"version" json:"version" yaml:"version" url:"version"`
Token string `form:"token" json:"token" yaml:"token" url:"token"`
Reason string `form:"reason" json:"reason" yaml:"reason" url:"reason"`
RemoteIP string `form:"-" json:"remoteIP" yaml:"-" url:"-"`
}
type UserList struct {
Count int `json:"count"`
List []Session `json:"list,omitempty"`
}
Go
1
https://gitee.com/h79/goim.git
git@gitee.com:h79/goim.git
h79
goim
goim
v0.3.7

搜索帮助