im

package module
v0.5.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2025 License: LGPL-3.0 Imports: 38 Imported by: 0

README

IM

一个基于websocket的聊天简单工具

可以分布式运行,一台机器也可以启几个,但需要改运行端口 服务器之间通过GRPC 通讯。

然后使用网页打开,比如:

http://127.0.0.1:7070/index?msg=1

http://127.0.0.1:7071/index?msg=1

http://127.0.0.1:7072/index?msg=1

相关GRPC测试,可以通过POSTMAN, img.png

Documentation

Index

Constants

View Source
const (
	CmdCreate     = 1
	CmdDestroy    = 2
	CmdClose      = 3
	CmdNotSession = 4
)
View Source
const (
	ETException = EventType(1)

	ETClientPing    = EventType(2)
	ETClientPingRsp = EventType(3)

	// ETClientBegin 客户端开始
	ETClientBegin        = EventType(10)
	ETClientHeartBeat    = EventType(10)
	ETClientHeartBeatRsp = EventType(11)

	// ETClientLogin 登录
	ETClientLogin    = EventType(100)
	ETClientLoginRsp = EventType(101)

	ETClientLogout    = EventType(102)
	ETClientLogoutRsp = EventType(103)

	// ETClientUserStatusChanged 用户状态发生变化
	ETClientUserStatusChanged    = EventType(104)
	ETClientUserStatusChangedRsp = EventType(105)

	ETClientUserList    = EventType(106)
	ETClientUserListRsp = EventType(107)

	// ETClientKicked 多端使用,被踢
	ETClientKicked    = EventType(108)
	ETClientKickedRsp = EventType(109)

	// ETMessage 消息事件
	ETMessage    = EventType(200)
	ETMessageRsp = EventType(201)

	ETNotify    = EventType(202)
	ETNotifyRsp = EventType(203)

	// ETChat 聊天事件
	ETChat    = EventType(204)
	ETChatRsp = EventType(205)

	// ETMq 简单的mq
	ETMq    = EventType(206)
	ETMqRsp = EventType(207)

	// ETBody 简单的消息
	ETBody    = EventType(208)
	ETBodyRsp = EventType(209)

	// ETClientEnd 客户端结束
	ETClientEnd = EventType(4999)

	// ETServerBegin 服务之间
	ETServerBegin = EventType(5000)

	ETServerEnd = EventType(10000)

	// ETExServerBegin 外部服务
	ETExServerBegin = EventType(20000)

	ETExServerEnd = EventType(30000)
)
View Source
const (
	MQMsg = iota + 1
	EVMsg
)
View Source
const (
	SubscribeMQ = iota
	UnSubscribeMQ
	SendMQ
)
View Source
const (
	SMsgLog = 1 << iota //发送
	RMsgLog             //接受
	HeartMsgLog
	AMsgLog = 255 //全部
)
View Source
const (
	StatusOffline   = 1
	StatusOnline    = 2
	StatusHeartbeat = 3
)
View Source
const HeartbeatDurationOpt = 10001
View Source
const (
	IgnoreStatusBroadcast = 1
)
View Source
const (
	LoginAction = Action("login")
)

Variables

View Source
var (
	EmptyByte = []byte("")
)
View Source
var GrcClientNil = result.Error(-102, "not exist rpc client")
View Source
var Ignored = result.Error(-103, "ignore the error")
View Source
var MQNotSupport = result.Error(-104, "not support MQ")
View Source
var NotExistRemoteServer = result.Error(-100, "not exist remote server")
View Source
var NotMemberErr = errors.New("is not member")
View Source
var StatusDesc = map[int32]string{
	StatusOffline: "离开",
	StatusOnline:  "在线",
}
View Source
var UserObjNil = result.Error(-101, "not exist remote user")

Functions

func AddResEventType added in v0.4.6

func AddResEventType(ev EventType)

AddResEventType not safe operation, best in process init

func BuildConn added in v0.5.6

func BuildConn(url string, head http.Header, resp func(resp *http.Response), opts ...OptionDial) (*websocket.Conn, error)

BuildConn for ws connect

func BytesToString added in v0.5.5

func BytesToString(b []byte) string

BytesToString []byte 转 string (零分配)

func DefaultRespInterceptor added in v0.4.28

func DefaultRespInterceptor(req *EventReq) bool

func IsClientEvent

func IsClientEvent(ev int32) bool

func IsEventType added in v0.4.6

func IsEventType(ev int32) int32

func IsExServerEvent

func IsExServerEvent(ev int32) bool

func IsMQEvent added in v0.2.11

func IsMQEvent(ev int32) bool

func IsServerEvent

func IsServerEvent(ev int32) bool

func IsUserStatusChanged

func IsUserStatusChanged(ev int32) bool

func NewContext added in v0.1.18

func NewContext(ctx context.Context, traceId string) context.Context

func NewWithTraceId added in v0.1.9

func NewWithTraceId(traceId string) context.Context

func PBSessionToSession

func PBSessionToSession(pb *pb.Session) session.Session

func PBUserToSession

func PBUserToSession(u *pb.User) session.Session

func Quote added in v0.5.5

func Quote(d interface{}) string

func RemoveResEventType added in v0.4.6

func RemoveResEventType(ev EventType)

RemoveResEventType not safe operation, best in process init

func SdkVersion added in v0.2.9

func SdkVersion() string

func SessionToPBSession

func SessionToPBSession(se *session.Session, token string) *pb.Session

func SessionToPBUser

func SessionToPBUser(session *session.Session) *pb.User

func StringEvent added in v0.1.9

func StringEvent(e int32) string

func StringToBytes added in v0.5.5

func StringToBytes(s string) []byte

StringToBytes string 转 []byte (零分配)

func ToPBEvent

func ToPBEvent(ev *Event) *pb.Event

func WithHeartbeatDuration added in v0.4.0

func WithHeartbeatDuration(t time.Duration) option.Option

Types

type Action added in v0.4.1

type Action string

type Alarm added in v0.4.0

type Alarm interface {
	Alarm(ctx context.Context, code int32, level int32, title, detail string, err error)
}

type AlarmFunc added in v0.2.5

type AlarmFunc func(ctx context.Context, code int32, level int32, title, detail string, err error)

func (AlarmFunc) Alarm added in v0.4.0

func (f AlarmFunc) Alarm(ctx context.Context, code int32, level int32, title, detail string, err error)

type Attribute added in v0.4.0

type Attribute map[string]any

type Body added in v0.5.5

type Body struct {
	EventType   int32  `json:"eventType"`   // 事件类型, EventType
	ContentType string `json:"contentType"` // Content 格式,json,xml...protobuff, 默认json
	Content     string `json:"content"`     // 具体的业务数据
}

type Client

type Client interface {
	GetSource() string
	GetIndex() int64
	GetHeartTime() int64
	GetSyncTime() int64
	GetConnectId() uint64
	GetAddr() string
	GetRemoteIP() string
	GetConnectTime() int64
	IsClosed() bool
	Close(ctx context.Context, reason string) error
	SendTo(msg []byte) error
	SendToQueue(ctx context.Context, msg []byte) error
}

type Config

type Config struct {
	WorkForceReset    bool  `json:"workForceReset" yaml:"workForceReset"`
	RegisterClient    int   `json:"registerClient" yaml:"registerClient"`       // 是否同步添加client
	HeartbeatDuration int64 `json:"heartbeatDuration" yaml:"heartbeatDuration"` //秒,client连接心跳检查间隔时长
	HeartbeatInterval int64 `json:"heartbeatInterval" yaml:"heartbeatInterval"` //秒,心跳同步时长
	UserStatusChange  bool  `json:"userStatusChange" yaml:"userStatusChange"`   //用户状态,是否需要通知
	Safe              bool  `json:"safe" yaml:"safe"`                           //是否安全方式,string to []byte OR []byte to string
}

type Connect

type Connect struct {
	// contains filtered or unexported fields
}

func NewConnect added in v0.4.0

func NewConnect(handler Handler, conn *websocket.Conn, version, remoteIp, addr string, index, firstTime int64, opts ...OptionConnect) *Connect

func (*Connect) AddSession added in v0.4.0

func (c *Connect) AddSession(key string, se *session.Session)

func (*Connect) Close added in v0.4.0

func (c *Connect) Close(ctx context.Context, reason string) error

func (*Connect) Enter added in v0.4.1

func (c *Connect) Enter(ctx context.Context, user *session.Session, loginTime int64)

func (*Connect) GetAddr added in v0.4.0

func (c *Connect) GetAddr() string

func (*Connect) GetConnectId added in v0.4.0

func (c *Connect) GetConnectId() uint64

func (*Connect) GetConnectTime added in v0.4.0

func (c *Connect) GetConnectTime() int64

func (*Connect) GetHeartTime added in v0.4.0

func (c *Connect) GetHeartTime() int64

func (*Connect) GetIndex added in v0.4.0

func (c *Connect) GetIndex() int64

func (*Connect) GetRemoteIP added in v0.4.0

func (c *Connect) GetRemoteIP() string

func (*Connect) GetSessionKey added in v0.4.0

func (c *Connect) GetSessionKey(fn func(s *session.Session) string) []string

func (*Connect) GetSource added in v0.4.0

func (c *Connect) GetSource() string

func (*Connect) GetSyncTime added in v0.4.0

func (c *Connect) GetSyncTime() int64

func (*Connect) HasSession added in v0.4.5

func (c *Connect) HasSession() int

func (*Connect) IsClosed added in v0.4.0

func (c *Connect) IsClosed() bool

func (*Connect) Leave added in v0.4.1

func (c *Connect) Leave(ctx context.Context, user *session.Session, logoutTime int64)

func (*Connect) RemoveSession added in v0.4.0

func (c *Connect) RemoveSession(key string) int

func (*Connect) Run added in v0.4.0

func (c *Connect) Run()

func (*Connect) SendMQ added in v0.4.20

func (c *Connect) SendMQ(ctx context.Context, session *session.Session, topic string, content string) error

SendMQ send mq pack

func (*Connect) SendTo added in v0.4.0

func (c *Connect) SendTo(msg []byte) error

func (*Connect) SendToQueue added in v0.4.0

func (c *Connect) SendToQueue(ctx context.Context, msg []byte) error

func (*Connect) String added in v0.4.0

func (c *Connect) String() string

func (*Connect) Subscribe added in v0.4.20

func (c *Connect) Subscribe(ctx context.Context, session *session.Session, topic ...string) error

Subscribe send subscribe pack

func (*Connect) UnSubscribe added in v0.4.20

func (c *Connect) UnSubscribe(ctx context.Context, session *session.Session, topic ...string) error

UnSubscribe send unsubscribe pack

type ConnectCmd added in v0.4.0

type ConnectCmd struct {
	Cmd     int
	Reason  string
	TraceId string
	Conn    *Connect
}

type Copy added in v0.1.3

type Copy struct {
	Begin   func(count int) interface{}
	Process func(be interface{}, index int, data interface{})
}

type DisposeInterceptor added in v0.4.7

type DisposeInterceptor func(conn *Connect, req *EventReq) (interface{}, error)

func ChainDispose added in v0.4.6

func ChainDispose(fn DisposeInterceptor) DisposeInterceptor

type Event

type Event struct {
	TraceId     string `json:"-"`
	EvTypeBase  int32  `json:"-"`           // 事件基数
	EventType   int32  `json:"eventType"`   // 事件类型, EventType
	ContentType string `json:"contentType"` // Content 格式,json,xml...protobuff, 默认json
	Content     string `json:"content"`     // 具体的业务数据
}

func CreateEvent

func CreateEvent(traceId string, evType EventType, content []byte) *Event

func CreateObjEvent added in v0.2.20

func CreateObjEvent(traceId string, evType EventType, content interface{}) *Event

func PBEventTo

func PBEventTo(ev *pb.Event) *Event

type EventReq

type EventReq struct {
	ReqHead
	session.Session
	Event
	// 事件相应的回应类型
	EvTypeRsp int32  `json:"-"`
	TraceId   string `json:"-"`
}

func Request

func Request(source string, user *session.Session) *EventReq

func RequestEvent

func RequestEvent(source string, ev *Event, user *session.Session) *EventReq

func (*EventReq) ToBytes

func (req *EventReq) ToBytes() []byte

func (*EventReq) ToSession

func (req *EventReq) ToSession() *session.Session

type EventRes

type EventRes struct {
	ResHead
	Event
}

func NewResponse

func NewResponse(source string, head request.HeadV2, result result.Result, ev *Event) *EventRes

func NewResponseWithError

func NewResponseWithError(source string, head request.HeadV2, err error, ev *Event) *EventRes

type EventType

type EventType int32

func (EventType) Int

func (m EventType) Int() int32

type FromMemberFunc added in v0.4.0

type FromMemberFunc func(from *session.Session, is bool) error

FromMemberFunc 是否是群成员, is= true 是成员

type Handler added in v0.5.4

type Handler interface {
	// HeartbeatSynDuration 心跳同步时长(单位秒)
	HeartbeatSynDuration() int64
	Source() string
	Process() Process
	// OnReady 链接准备好了
	OnReady(ctx context.Context, conn *Connect)
	// OnClosing close之前
	OnClosing(ctx context.Context, conn *Connect)
	OnClosed(ctx context.Context, conn *Connect)
	OnDestroy(ctx context.Context, conn *Connect)
	// OnHeartbeat websocket发生心跳的回调
	OnHeartbeat(ctx context.Context, conn *Connect, timeAt int64)
	// OnLogin 用户
	OnLogin(ctx context.Context, user *session.Session, conn *Connect, timeAt int64)
	OnLogout(ctx context.Context, user *session.Session, conn *Connect, timeAt int64)
}

type Hub added in v0.2.11

type Hub struct {
	// contains filtered or unexported fields
}

func Inst

func Inst() *Hub

func NewHub added in v0.2.11

func NewHub(cfg Config, workMod int, opts ...Option) *Hub

func NewInstance added in v0.5.3

func NewInstance(cfg Config, workMod int, opts ...Option) *Hub

func (*Hub) AddConnect added in v0.4.0

func (hub *Hub) AddConnect(conn *Connect)

func (*Hub) AddUser added in v0.4.22

func (hub *Hub) AddUser(userKey string, client *UserClient)

func (*Hub) Alarm added in v0.2.11

func (hub *Hub) Alarm(ctx context.Context, code int32, level int32, title, detail string, err error)

func (*Hub) CheckAllUserOnline added in v0.2.11

func (hub *Hub) CheckAllUserOnline(ctx context.Context, session *session.Session) bool

func (*Hub) CheckUserOnline added in v0.2.11

func (hub *Hub) CheckUserOnline(ctx context.Context, se *session.Session) bool

CheckUserOnline 查询用户是否在线

func (*Hub) ConnectQuery added in v0.4.0

func (hub *Hub) ConnectQuery(f func(client *Connect) (result bool))

func (*Hub) ConnectSearch added in v0.4.0

func (hub *Hub) ConnectSearch(q Query) []*Connect

func (*Hub) DelConnect added in v0.4.0

func (hub *Hub) DelConnect(conn *Connect)

func (*Hub) DelUser added in v0.2.11

func (hub *Hub) DelUser(ctx context.Context, user *session.Session, conn *Connect) bool

func (*Hub) GetAllUserList added in v0.2.11

func (hub *Hub) GetAllUserList(ctx context.Context, session *session.Session) *UserList

GetAllUserList 查询所有用户

func (*Hub) GetAppList added in v0.3.5

func (hub *Hub) GetAppList() []string

func (*Hub) GetConnects added in v0.4.0

func (hub *Hub) GetConnects() []*Connect

func (*Hub) GetLocalUserList added in v0.2.11

func (hub *Hub) GetLocalUserList(ctx context.Context, session *session.Session) *UserList

GetLocalUserList 本机的用户

func (*Hub) GetOnline added in v0.2.21

func (hub *Hub) GetOnline(ctx context.Context, session *session.Session, online int) *OnLineList

GetOnline 在线或离线用户 online =1 在线;=2 离线 3=

func (*Hub) GetServerHost added in v0.2.11

func (hub *Hub) GetServerHost() string

func (*Hub) GetServerList added in v0.5.11

func (hub *Hub) GetServerList() []builder.Address

func (*Hub) GetServerPort added in v0.2.11

func (hub *Hub) GetServerPort() int

func (*Hub) GetUser added in v0.4.0

func (hub *Hub) GetUser(key string, session *session.Session) *UserClient

func (*Hub) Handler added in v0.2.11

func (hub *Hub) Handler(ctx context.Context, version, remoteIP string, w http.ResponseWriter, r *http.Request) (*Connect, error)

func (*Hub) HeartbeatSynDuration added in v0.4.0

func (hub *Hub) HeartbeatSynDuration() int64

func (*Hub) InConnect added in v0.4.0

func (hub *Hub) InConnect(conn *Connect) (ok bool)

func (*Hub) IsLocalAddress added in v0.2.11

func (hub *Hub) IsLocalAddress(server string) bool

func (*Hub) OnClosed added in v0.4.5

func (hub *Hub) OnClosed(ctx context.Context, conn *Connect)

func (*Hub) OnClosing added in v0.4.5

func (hub *Hub) OnClosing(ctx context.Context, conn *Connect)

func (*Hub) OnDestroy added in v0.4.5

func (hub *Hub) OnDestroy(ctx context.Context, conn *Connect)

func (*Hub) OnHeartbeat added in v0.4.0

func (hub *Hub) OnHeartbeat(ctx context.Context, conn *Connect, timeAt int64)

func (*Hub) OnLogin added in v0.2.11

func (hub *Hub) OnLogin(ctx context.Context, user *session.Session, conn *Connect, timeAt int64)

func (*Hub) OnLogout added in v0.2.11

func (hub *Hub) OnLogout(ctx context.Context, user *session.Session, conn *Connect, timeAt int64)

func (*Hub) OnReady added in v0.4.9

func (hub *Hub) OnReady(ctx context.Context, conn *Connect)

func (*Hub) Process added in v0.5.1

func (hub *Hub) Process() Process

func (*Hub) RateLimit added in v0.2.11

func (hub *Hub) RateLimit(ctx context.Context, session *session.Session, version string) error

func (*Hub) Receive added in v0.2.11

func (hub *Hub) Receive(ctx context.Context, from *session.Session, to *session.Session, ev *Event) (err error)

func (*Hub) ReceiveAll added in v0.2.11

func (hub *Hub) ReceiveAll(ctx context.Context, from *session.Session, to *session.Session, ev *Event) (err error)

func (*Hub) SendChat added in v0.2.11

func (hub *Hub) SendChat(ctx context.Context, from *session.Session, ev *Event, option chat.Option, handler func()) error

func (*Hub) SendToAllUser added in v0.2.11

func (hub *Hub) SendToAllUser(ctx context.Context, from *session.Session, to *session.Session, ev *Event)

SendToAllUser 给全体用户发消息

func (*Hub) SendToLocalAppId added in v0.2.11

func (hub *Hub) SendToLocalAppId(ctx context.Context, from *session.Session, to *session.Session, ev *Event, payload any)

SendToLocalAppId 给本地 指定的客户端appid 送消息到, 除了userId payload 用户定义的数据结构(内部使用)

func (*Hub) SendToLocalUser added in v0.2.11

func (hub *Hub) SendToLocalUser(ctx context.Context, from *session.Session, to *session.Session, ev *Event) (error, bool)

SendToLocalUser 给本机指定用户发送消息

func (*Hub) SendToRemote added in v0.2.11

func (hub *Hub) SendToRemote(ctx context.Context, from *session.Session, to *session.Session, ev *Event)

SendToRemote 发送到远端

func (*Hub) SendToRemoteAppId added in v0.2.11

func (hub *Hub) SendToRemoteAppId(ctx context.Context, addr string, from *session.Session, to *session.Session, ev *Event)

func (*Hub) SendToRemoteUser added in v0.2.11

func (hub *Hub) SendToRemoteUser(ctx context.Context, from *session.Session, to *session.Session, ev *Event) error

func (*Hub) SendToUser added in v0.2.11

func (hub *Hub) SendToUser(ctx context.Context, from *session.Session, to *session.Session, ev *Event) error

SendToUser 给指定在线用户发送消息

func (*Hub) Source added in v0.2.11

func (hub *Hub) Source() string

func (*Hub) Start added in v0.5.2

func (hub *Hub) Start(addr server.Address)

func (*Hub) StatusChanged added in v0.2.11

func (hub *Hub) StatusChanged(ctx context.Context, us Status)

func (*Hub) Stop added in v0.5.2

func (hub *Hub) Stop()

func (*Hub) SyncToRemoteOnly added in v0.2.11

func (hub *Hub) SyncToRemoteOnly(ctx context.Context, from *session.Session, to *session.Session, ev *Event)

SyncToRemoteOnly 仅仅只同步到远端

func (*Hub) UseClientQuery added in v0.4.0

func (hub *Hub) UseClientQuery(keys ...string) []*UserClient

func (*Hub) UseSafe added in v0.5.5

func (hub *Hub) UseSafe() bool

type MemberFunc

type MemberFunc func(groupId int64, member *session.Session) error

type Message added in v0.5.5

type Message struct {
	AppId    string `json:"appId"`
	Account  string `json:"account"`
	UserId   int64  `json:"userId"`
	Plat     int32  `json:"plat"`
	TenantId int32  `json:"tenantId,omitempty"`
	OrgId    int32  `json:"orgId,omitempty"`
	Body     Body   `json:"body"`
}

func (*Message) HasUser added in v0.5.5

func (m *Message) HasUser() bool

func (*Message) Session added in v0.5.5

func (m *Message) Session() session.Session

type MessageMQ added in v0.2.11

type MessageMQ struct {
	Cmd     int    `json:"cmd"`
	Flag    int32  `json:"flag"`    // 是否排除自己
	Topic   string `json:"topic"`   // SubscribeMQ OR UnSubscribeMQ 可以多个topic, 以逗号分隔
	Content string `json:"content"` // 具体内容
	// contains filtered or unexported fields
}

type Mq added in v0.2.11

type Mq struct {
	// contains filtered or unexported fields
}

func (*Mq) Process added in v0.2.11

func (mq *Mq) Process(c *Connect, req *EventReq) (interface{}, error)

func (*Mq) Receive added in v0.2.11

func (mq *Mq) Receive(ctx context.Context, from *session.Session, ev *Event)

type MsgInterceptor added in v0.4.0

type MsgInterceptor interface {
	MsgInterceptor(ctx context.Context, from session.Session, to *Connect, payloadType int, payload any) error
}

type MsgInterceptorFunc added in v0.4.0

type MsgInterceptorFunc func(ctx context.Context, from session.Session, to *Connect, payloadType int, payload any) error

func (MsgInterceptorFunc) MsgInterceptor added in v0.4.0

func (f MsgInterceptorFunc) MsgInterceptor(ctx context.Context, from session.Session, to *Connect, payloadType int, payload any) error

type OnLineList added in v0.2.21

type OnLineList struct {
	Count int       `json:"count"`
	List  []*Online `json:"list,omitempty"`
}

type Online

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
	Version       string `json:"version"`    // 版本信息
	Name          string `json:"name"`       // 名称
	Account       string `json:"account"`
	UserId        int64  `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 (*Online) Heartbeat

func (u *Online) Heartbeat(currentTime int64)

func (*Online) IsLocal

func (u *Online) IsLocal(localIp string, localPort int) bool

IsLocal 用户是否在本台机器上

func (*Online) IsOnline

func (u *Online) IsOnline(heartCheck func(heartbeat int64) bool) bool

func (*Online) LogIn

func (u *Online) LogIn(currentTime int64)

func (*Online) LogOut

func (u *Online) LogOut(currentTime int64)

func (*Online) String

func (u *Online) String() string

func (*Online) ToSession added in v0.2.21

func (u *Online) ToSession() session.Session

type Option

type Option func(hub *Hub)

func WithAlarmOption added in v0.4.0

func WithAlarmOption(a Alarm) Option

func WithChatGroupOption added in v0.2.11

func WithChatGroupOption(group chat.Group) Option

WithChatGroupOption 群管理及群聊

func WithChatOption added in v0.2.15

func WithChatOption(chat chat.Chat) Option

WithChatOption 聊天信息保存

func WithDiscoveryClientOption added in v0.2.15

func WithDiscoveryClientOption(client discoveryservice.Client) Option

WithDiscoveryClientOption 分布式支持

func WithMsgInterceptorOption added in v0.4.0

func WithMsgInterceptorOption(a MsgInterceptor) Option

func WithProcessOption added in v0.5.1

func WithProcessOption(p Process) Option

WithProcessOption 处理器

func WithRpcClientOption added in v0.2.11

func WithRpcClientOption(rpc rpc.Client) Option

WithRpcClientOption 外部服务接用

func WithTargetOption added in v0.5.11

func WithTargetOption(t builder.Target) Option

func WithUpgraderCheckOriginOption added in v0.4.0

func WithUpgraderCheckOriginOption(checkOrigin func(r *http.Request) bool) Option

func WithUpgraderEnableCompressionOption added in v0.4.0

func WithUpgraderEnableCompressionOption(enable bool) Option

func WithUpgraderErrOption added in v0.4.0

func WithUpgraderErrOption(err func(w http.ResponseWriter, r *http.Request, status int, reason error)) Option

func WithUpgraderHandshakeTimeoutOption added in v0.4.0

func WithUpgraderHandshakeTimeoutOption(d time.Duration) Option

func WithUpgraderReadBufferSizeOption added in v0.4.0

func WithUpgraderReadBufferSizeOption(size int) Option

func WithUpgraderSubprotocolsOption added in v0.4.0

func WithUpgraderSubprotocolsOption(subprotocols []string) Option

func WithUpgraderWriteBufferSizeOption added in v0.4.0

func WithUpgraderWriteBufferSizeOption(size int) Option

func WithUseMQOption added in v0.2.11

func WithUseMQOption() Option

WithUseMQOption MQ支持

func WithUserMgrOption added in v0.2.11

func WithUserMgrOption(user User) Option

WithUserMgrOption 用户管理

func WithWorkerNumOption added in v0.2.13

func WithWorkerNumOption(work int) Option

WithWorkerNumOption 配置工作线程

type OptionConnect added in v0.4.1

type OptionConnect func(conn *Connect)

func WithConnHeartbeatDuration added in v0.4.1

func WithConnHeartbeatDuration(t int64) OptionConnect

type OptionDial added in v0.4.7

type OptionDial func(dia *websocket.Dialer)

func WithWsEnableCompressionOption added in v0.4.7

func WithWsEnableCompressionOption(enable bool) OptionDial

func WithWsHandshakeTimeout added in v0.4.7

func WithWsHandshakeTimeout(t time.Duration) OptionDial

func WithWsJarOption added in v0.4.7

func WithWsJarOption(jar http.CookieJar) OptionDial

func WithWsProxyOption added in v0.4.7

func WithWsProxyOption(proxy func(*http.Request) (*url.URL, error)) OptionDial

func WithWsReadBufferSizeOption added in v0.4.7

func WithWsReadBufferSizeOption(size int) OptionDial

func WithWsSubprotocolsOption added in v0.4.7

func WithWsSubprotocolsOption(subprotocols []string) OptionDial

func WithWsTlsOption added in v0.4.7

func WithWsTlsOption(config *tls.Config) OptionDial

func WithWsWriteBufferSizeOption added in v0.4.7

func WithWsWriteBufferSizeOption(size int) OptionDial

type Process added in v0.5.1

type Process interface {
	SetMessageLog(log int)
	GetMessageLog() int
	HasMessageLog(log int) bool
	PrintLog(evType int32) bool
	SetRespInterceptor(fn RespInterceptorFunc)
	GetRespInterceptor() RespInterceptorFunc
	SetUnaryDisposeInterceptor(fn UnaryDisposeInterceptor)
	GetUnaryDisposeInterceptor() UnaryDisposeInterceptor
	RegisterDispose(key int32, fn DisposeInterceptor)
	GetDispose(key int32) (DisposeInterceptor, bool)
}

func NewProcess added in v0.5.1

func NewProcess() Process

type Query added in v0.3.8

type Query interface {
	Search(client *Connect) int
}

type QueryFunc added in v0.3.8

type QueryFunc func(client *Connect) int

func (QueryFunc) Search added in v0.3.8

func (f QueryFunc) Search(client *Connect) int

type ReqHead

type ReqHead struct {
	Source  string        `json:"source"`
	Version string        `json:"version"`
	SeqId   string        `json:"seqId"`
	ReqAt   int64         `json:"timeAt"` //客户端或服务器的时间
	RecvAt  int64         `json:"-"`      //服务器收到的时间
	TimeOut time.Duration `json:"-"`
}

func (*ReqHead) GetRecvAt

func (h *ReqHead) GetRecvAt() int64

GetRecvAt request.HeadV2 interface

func (*ReqHead) GetReqAt

func (h *ReqHead) GetReqAt() int64

GetReqAt request.HeadV2 interface

func (*ReqHead) GetSeqId

func (h *ReqHead) GetSeqId() string

GetSeqId request.Head interface

func (*ReqHead) GetSource

func (h *ReqHead) GetSource() string

GetSource request.Head interface

func (*ReqHead) GetTimeOut

func (h *ReqHead) GetTimeOut() time.Duration

GetTimeOut request.HeadV2 interface

func (*ReqHead) GetVersion

func (h *ReqHead) GetVersion() string

GetVersion request.Head interface

func (*ReqHead) SetSource

func (h *ReqHead) SetSource(source string)

SetSource request.HeadV2 interface

func (*ReqHead) SetTimeOut

func (h *ReqHead) SetTimeOut(duration time.Duration)

SetTimeOut request.HeadV2 interface

type ResCmd added in v0.5.10

type ResCmd struct {
	Cmd  int    `json:"cmd"`
	Code int32  `json:"code"`
	Msg  string `json:"msg"`
}

type ResHead

type ResHead struct {
	result.Result
	Version  string `json:"version"`            //版本,请求者版本
	Source   string `json:"source,omitempty"`   //请求源,可以请求者填写
	SeqId    string `json:"seqId,omitempty"`    //请求序号,由请求者定义,服务器原路返回
	TimeAt   int64  `json:"timeAt,omitempty"`   //服务收到请求的时间(ms)
	DiffAt   int64  `json:"diffAt,omitempty"`   //客户端与服务端时间差值(ms)
	SpendAt  int64  `json:"spendAt,omitempty"`  //从服务收到请求到响应完成,所花的时长(ms)
	ServerAt int64  `json:"serverAt,omitempty"` //服务器时间,豪秒,用于检验对时(ms)
	LogId    string `json:"logId,omitempty"`
}

func NewResHead

func NewResHead(source string, res result.Result) ResHead

type RespInterceptor added in v0.4.6

type RespInterceptor interface {
	RespInterceptor(req *EventReq) bool
}

RespInterceptor 回复拦截器

type RespInterceptorFunc added in v0.4.6

type RespInterceptorFunc func(req *EventReq) bool

func ChainRespInterceptor added in v0.4.28

func ChainRespInterceptor(fn RespInterceptorFunc) RespInterceptorFunc

func (RespInterceptorFunc) RespInterceptor added in v0.4.6

func (f RespInterceptorFunc) RespInterceptor(req *EventReq) bool

type Status

type Status struct {
	session.Session
	LoginTime  int64  `json:"-"`
	TimeAt     int64  `json:"timeAt"`
	Status     int32  `json:"status"`
	Desc       string `json:"desc"`
	Version    string `json:"-"`
	TraceId    string `json:"-"`
	Addr       string `json:"-"`
	RemoteAddr string `json:"-"`
}

func (*Status) String

func (s *Status) String() string

type UnaryDisposeInterceptor added in v0.4.7

type UnaryDisposeInterceptor func(conn *Connect, req *EventReq, handler DisposeInterceptor) (resp any, err error)

type User

type User interface {
	// HeartbeatTimeout (秒)用户在线心跳超时时长
	HeartbeatTimeout() int64

	SetSupportApp(apps []session.Terminal)
	NextSupportApp(ctx context.Context, fn func(app session.Terminal) bool)
	ExistSupportApp(ctx context.Context, app session.Terminal) bool
	// RateLimit 是否限速
	RateLimit(ctx context.Context, session *session.Session, version string) error
	GetUserKey(session *session.Session) string
	// AllowUser 是否允许
	AllowUser(ctx context.Context, session *session.Session, action Action) (Attribute, error)
	// GetUserOnline 获取所有 online =1 在线;=2 离线 3=
	GetUserOnline(ctx context.Context, session *session.Session, online int) (*Online, error)
	// SetUserOnline 可以保存到redis
	SetUserOnline(ctx context.Context, session *session.Session, online *Online) error
	RemoveUserOnline(ctx context.Context, session *session.Session) error
	IsUserOnline(ctx context.Context, session *session.Session) (bool, error)
	// GetAllOnline 获取所有 online =1 在线;=2 离线 3=
	GetAllOnline(ctx context.Context, cc *Copy, session *session.Session, online int) error

	//UserStatusChanged 通知其它服务有变化
	UserStatusChanged(ctx context.Context, status *Status)
}

type UserClient added in v0.4.0

type UserClient struct {
	*session.Session
	// contains filtered or unexported fields
}

func BuildClient added in v0.1.22

func BuildClient(conn *Connect, userKey string, se *session.Session, loginTime int64) *UserClient

func (*UserClient) Close added in v0.4.2

func (c *UserClient) Close(ctx context.Context, reason string) error

func (*UserClient) EqualConnect added in v0.4.0

func (c *UserClient) EqualConnect(conn *Connect) bool

func (*UserClient) GetAddr added in v0.4.0

func (c *UserClient) GetAddr() string

func (*UserClient) GetConnect added in v0.4.2

func (c *UserClient) GetConnect() *Connect

func (*UserClient) GetConnectId added in v0.4.0

func (c *UserClient) GetConnectId() uint64

func (*UserClient) GetConnectTime added in v0.4.0

func (c *UserClient) GetConnectTime() int64

func (*UserClient) GetHeartTime added in v0.4.0

func (c *UserClient) GetHeartTime() int64

func (*UserClient) GetIndex added in v0.4.0

func (c *UserClient) GetIndex() int64

func (*UserClient) GetRemoteIP added in v0.4.0

func (c *UserClient) GetRemoteIP() string

func (*UserClient) GetSession added in v0.4.0

func (c *UserClient) GetSession() session.Session

func (*UserClient) GetSource added in v0.4.0

func (c *UserClient) GetSource() string

func (*UserClient) GetSyncTime added in v0.4.0

func (c *UserClient) GetSyncTime() int64

func (*UserClient) IsClosed added in v0.4.0

func (c *UserClient) IsClosed() bool

func (*UserClient) IsLogin added in v0.4.0

func (c *UserClient) IsLogin() bool

func (*UserClient) Login added in v0.4.0

func (c *UserClient) Login(ctx context.Context) error

Login send login pack

func (*UserClient) Logout added in v0.4.0

func (c *UserClient) Logout(ctx context.Context) error

Logout send logout pack

func (*UserClient) Send added in v0.4.0

func (c *UserClient) Send(ctx context.Context, eventType EventType, body []byte) error

func (*UserClient) SendMQ added in v0.4.0

func (c *UserClient) SendMQ(ctx context.Context, topic string, content string) error

SendMQ send mq pack

func (*UserClient) SendTo added in v0.4.0

func (c *UserClient) SendTo(msg []byte) error

func (*UserClient) SendToQueue added in v0.4.0

func (c *UserClient) SendToQueue(ctx context.Context, msg []byte) error

func (*UserClient) String added in v0.4.0

func (c *UserClient) String() string

func (*UserClient) Subscribe added in v0.4.0

func (c *UserClient) Subscribe(ctx context.Context, topic ...string) error

Subscribe send subscribe pack

func (*UserClient) UnSubscribe added in v0.4.0

func (c *UserClient) UnSubscribe(ctx context.Context, topic ...string) error

UnSubscribe send unsubscribe pack

type UserList

type UserList struct {
	Count int               `json:"count"`
	List  []session.Session `json:"list,omitempty"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL