im

package module
v0.1.22 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2023 License: LGPL-3.0 Imports: 36 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 (
	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)
	ETClientUserStatusChangedRes = EventType(105)

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

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

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

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

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

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

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

	ETServerEnd = EventType(10000)

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

	ETExServerEnd = EventType(30000)

	// ETHungUp 电话区间
	ETHungUp    = EventType(50000)
	ETHungUpRsp = EventType(50001)

	ETAppCall    = EventType(50002)
	ETAppCallRsp = EventType(50003)

	// ETCallTransfer 将当前通道转接到某个用户
	ETCallTransfer    = EventType(50004)
	ETCallTransferRsp = EventType(50005)

	// ETCallListen 监听电话
	ETCallListen    = EventType(50006)
	ETCallListenRsp = EventType(50007)

	ETCallout2    = EventType(50008)
	ETCallout2Rsp = EventType(50009)

	ETCallout    = EventType(50010)
	ETCalloutRsp = EventType(50011)
)
View Source
const (
	CmdRegister   = 1
	CmdUnRegister = -1
	CmdClose      = -2
)
View Source
const (
	StatusOffline   = 1
	StatusOnline    = 2
	StatusHeartbeat = 3
)
View Source
const UserHeartbeatTimeout = int64(time.Minute * 3)

UserHeartbeatTimeout 3分钟

Variables

View Source
var GrcClientNil = result.Error(-102, "not exist remote server")
View Source
var IAmNotMemberErr = errors.New("i am not member")
View Source
var (
	KEmptyByte = []byte("")
)
View Source
var NotExistRemoteServer = result.Error(-100, "not exist remote server")
View Source
var StatusDesc = map[int32]string{
	StatusOffline: "离开",
	StatusOnline:  "在线",
}
View Source
var UserObjNil = result.Error(-101, "not exist remote server")

Functions

func ContentQuote

func ContentQuote(d interface{}) string

func IsClientEvent

func IsClientEvent(ev int32) bool

func IsExServerEvent

func IsExServerEvent(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 Register

func Register(key int32, value DisposeFunc)

func SessionToPBSession

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

func SessionToPBUser

func SessionToPBUser(session *Session) *pb.User

func StringEvent added in v0.1.9

func StringEvent(e int32) string

func ToPBEvent

func ToPBEvent(ev *Event) *pb.Event

func WithHandshakeTimeout added in v0.1.22

func WithHandshakeTimeout(t time.Duration) option.Option

Types

type App

type App struct {
	AppId string `form:"appId" binding:"required" json:"appId"`
	Plat  int32  `form:"plat" binding:"-" json:"plat"`
}

type Client

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

func BuildClient added in v0.1.22

func BuildClient(im Manager, connect Connect, url string, index, firstTime int64, resp func(resp *http.Response), opts ...option.Option) *Client

BuildClient for client

func NewClient

func NewClient(im Manager, conn *websocket.Conn, connect Connect, addr string, index, firstTime int64) *Client

func (*Client) Close added in v0.1.22

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

func (*Client) GetSession added in v0.1.10

func (c *Client) GetSession() Session

func (*Client) GetToken added in v0.1.10

func (c *Client) GetToken() string

func (*Client) Heartbeat

func (c *Client) Heartbeat(ctx context.Context, currentTime int64)

func (*Client) IsClosed

func (c *Client) IsClosed() bool

func (*Client) IsLogin

func (c *Client) IsLogin() bool

func (*Client) Kick added in v0.1.10

func (c *Client) Kick(ctx context.Context, reason string)

func (*Client) Login

func (c *Client) Login(ctx context.Context, user *Session, loginTime int64)

func (*Client) Logout

func (c *Client) Logout(ctx context.Context, logoutTime int64)

func (*Client) Run added in v0.1.22

func (c *Client) Run()

func (*Client) SendTo

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

func (*Client) SendToQueue

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

func (*Client) String

func (c *Client) String() string

func (*Client) WithDisposeFunc added in v0.1.22

func (c *Client) WithDisposeFunc(fn DisposeFunc) *Client

type ClientCmd added in v0.1.22

type ClientCmd struct {
	Cmd     int
	Reason  string
	TraceId string
	Client  *Client
}

type Config

type Config struct {
	WorkForceReset bool                                                          `json:"workForceReset" yaml:"workForceReset"`
	RegisterClient int                                                           `json:"registerClient" yaml:"registerClient"`
	ServerAddr     server.Address                                                `json:"serverAddr" yaml:"serverAddr"` //服务地址
	CheckFunc      func(ctx context.Context, client *Client, token string) error `json:"-" yaml:"-"`
	// contains filtered or unexported fields
}

type Connect

type Connect struct {
	Session
	Version  string `form:"version" json:"version"`
	Token    string `form:"token" json:"token"`
	RemoteIP string `form:"remoteIP" json:"remoteIP"`
}

type Copy added in v0.1.3

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

type DisposeFunc

type DisposeFunc func(client *Client, req *EventReq) (interface{}, error)

type Event

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

func CreateEvent

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

func CreateEventWithContent

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

func PBEventTo

func PBEventTo(ev *pb.Event) *Event

type EventReq

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

func Request

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

func RequestEvent

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

func (*EventReq) ToBytes

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

func (*EventReq) ToSession

func (req *EventReq) ToSession() *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 IAmMemberFunc

type IAmMemberFunc func(user *Session, iAmMember bool) error

IAmMemberFunc 是否是群成员, iAmMember= true 是成员

type IM

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

func Inst

func Inst() *IM

func NewIM

func NewIM(workMod int) *IM

func (*IM) AddClients

func (im *IM) AddClients(client *Client)

func (*IM) AddUsers

func (im *IM) AddUsers(ctx context.Context, key string, client *Client) int

func (*IM) Alarm added in v0.1.10

func (im *IM) Alarm(ctx context.Context, detail string, err error)

func (*IM) CheckAllUserOnline

func (im *IM) CheckAllUserOnline(session *Session) bool

func (*IM) CheckUserOnline

func (im *IM) CheckUserOnline(session *Session) bool

CheckUserOnline 查询用户是否在线

func (*IM) ClearTimeoutConnections

func (im *IM) ClearTimeoutConnections()

func (*IM) ClientsRange

func (im *IM) ClientsRange(f func(client *Client) (result bool))

func (*IM) DelClients

func (im *IM) DelClients(client *Client)

func (*IM) DelUsers

func (im *IM) DelUsers(ctx context.Context, client *Client) bool

func (*IM) DoClientCmd added in v0.1.22

func (im *IM) DoClientCmd(ctx context.Context, reg *ClientCmd)

func (*IM) DoClientCmdSync added in v0.1.22

func (im *IM) DoClientCmdSync(ctx context.Context, reg *ClientCmd)

func (*IM) GetAllUserList

func (im *IM) GetAllUserList(session *Session) *UserList

GetAllUserList 查询所有用户

func (*IM) GetClients

func (im *IM) GetClients() []*Client

func (*IM) GetLocalUserList

func (im *IM) GetLocalUserList(session *Session) *UserList

GetLocalUserList 本机的用户

func (*IM) GetServerHost

func (im *IM) GetServerHost() string

func (*IM) GetServerPort

func (im *IM) GetServerPort() int

func (*IM) GetServers

func (im *IM) GetServers() []builder.Address

func (*IM) GetUserClient

func (im *IM) GetUserClient(session *Session) *Client

func (*IM) GrpcClient

func (im *IM) GrpcClient() rpc.IMClient

func (*IM) Handler

func (im *IM) Handler(ctx context.Context, connect Connect, w http.ResponseWriter, r *http.Request) error

func (*IM) InClient

func (im *IM) InClient(client *Client) (ok bool)

func (*IM) Init

func (im *IM) Init(client discoveryservice.Client, rpcClient rpc.IMClient, user User, chat chat.Group, cfg Config)

func (*IM) IsLocalAddress

func (im *IM) IsLocalAddress(server string) bool

func (*IM) Kicked added in v0.1.22

func (im *IM) Kicked(ctx context.Context, client *Client, reason string)

func (*IM) OnLogin added in v0.1.22

func (im *IM) OnLogin(ctx context.Context, log *UserLog)

OnLogin 用户登录

func (*IM) OnLogout added in v0.1.22

func (im *IM) OnLogout(ctx context.Context, log *UserLog)

OnLogout 用户登出

func (*IM) RateLimit

func (im *IM) RateLimit(session Session, version string) bool

func (*IM) ReceiveFromExServer

func (im *IM) ReceiveFromExServer(ctx context.Context, from *Session, to *Session, ev *Event, defHandler func())

ReceiveFromExServer 收到外服务器的消息

func (*IM) ReceiveToLocal

func (im *IM) ReceiveToLocal(ctx context.Context, from *Session, to *Session, ev *Event)

ReceiveToLocal 收到服务器之间的消息

func (*IM) SendChat

func (im *IM) SendChat(ctx context.Context, from *Session, ev *Event, option chat.Option, handler func()) error

func (*IM) SendToAllUser

func (im *IM) SendToAllUser(ctx context.Context, from *Session, to *Session, ev *Event, custom interface{})

SendToAllUser 给全体用户发消息

func (*IM) SendToLocalAppId

func (im *IM) SendToLocalAppId(ctx context.Context, from *Session, to *Session, ev *Event, custom interface{})

SendToLocalAppId 给本地 指定的客户端appid 送消息到, 除了userId

func (*IM) SendToLocalUser

func (im *IM) SendToLocalUser(ctx context.Context, from *Session, to *Session, ev *Event) (error, bool)

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

func (*IM) SendToRemote

func (im *IM) SendToRemote(ctx context.Context, from *Session, to *Session, ev *Event)

SendToRemote 发送到远端

func (*IM) SendToRemoteAppId

func (im *IM) SendToRemoteAppId(ctx context.Context, addr string, from *Session, to *Session, ev *Event)

func (*IM) SendToRemoteUser

func (im *IM) SendToRemoteUser(ctx context.Context, from *Session, to *Session, ev *Event) error

func (*IM) SendToUser

func (im *IM) SendToUser(ctx context.Context, from *Session, to *Session, ev *Event) error

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

func (*IM) Source added in v0.1.22

func (im *IM) Source() string

func (*IM) SyncToRemoteOnly

func (im *IM) SyncToRemoteOnly(ctx context.Context, from *Session, to *Session, ev *Event)

SyncToRemoteOnly 仅仅只同步到远端

func (*IM) UnInit

func (im *IM) UnInit()

func (*IM) UserMgr

func (im *IM) UserMgr() User

func (*IM) UserStatusChanged added in v0.1.22

func (im *IM) UserStatusChanged(ctx context.Context, us *UserStatus)

func (*IM) WithGroupModel

func (im *IM) WithGroupModel(chat chat.Group) *IM

func (*IM) WithGrpcClient

func (im *IM) WithGrpcClient(grpc rpc.IMClient) *IM

func (*IM) WithUserMgr

func (im *IM) WithUserMgr(user User) *IM

type Manager added in v0.1.22

type Manager interface {
	DoClientCmd(ctx context.Context, reg *ClientCmd)
	DoClientCmdSync(ctx context.Context, reg *ClientCmd)
	Kicked(ctx context.Context, client *Client, reason string)
	UserStatusChanged(ctx context.Context, us *UserStatus)
	OnLogin(ctx context.Context, log *UserLog)
	OnLogout(ctx context.Context, log *UserLog)
	Source() string
}

type MemberFunc

type MemberFunc func(groupId int64, user *Session) error

type MsgPack added in v0.1.9

type MsgPack struct {
	TraceId string
	Body    []byte
}

type Online

type Online struct {
	AppId  string `json:"appId"`
	UserId uint64 `json:"userId"`
	Plat   int32  `json:"plat"`
	// 用户所在的服务器ip
	Host string `json:"host,omitempty"`
	// 用户所在的服务器端口
	Port int `json:"port,omitempty"`
	// 客户端Ip
	ClientIp string `json:"clientIp,omitempty"`
	// 客户端端口
	ClientPort string `json:"clientPort,omitempty"`
	// 用户上次登录时间
	LoginTime int64 `json:"loginTime,omitempty"`
	// 用户上次心跳时间
	HeartbeatTime int64 `json:"heartbeatTime,omitempty"`
	// 用户退出登录的时间
	LogOutTime int64 `json:"logOutTime,omitempty"`
	// 是否离线
	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

type Option

type Option struct {
	CrmNotifyEnable bool
}

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 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 Session

type Session struct {
	AppId    string `form:"appId" binding:"required" json:"appId"`
	Plat     int32  `form:"plat" binding:"-" json:"plat"`
	UserId   uint64 `form:"userId" binding:"required" json:"userId"`
	ClientId uint64 `form:"clientId" binding:"-" json:"clientId"`
}

func PBSessionToSession

func PBSessionToSession(session *pb.Session) Session

func PBUserToSession

func PBUserToSession(u *pb.User) Session

func (*Session) IsSameAppId added in v0.1.12

func (s *Session) IsSameAppId(session *Session) bool

func (*Session) IsSamePlat added in v0.1.12

func (s *Session) IsSamePlat(session *Session) bool

func (*Session) String

func (s *Session) String() string

type Status

type Status struct {
	Session
	TimeAt  int64  `json:"timeAt"`
	Status  int32  `json:"status"`
	Desc    string `json:"desc"`
	TraceId string `json:"-"`
}

func (*Status) String

func (s *Status) String() string

type User

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) (*Online, error)
	// SetUserOnline 写到redis中
	SetUserOnline(session *Session, online *Online) error
	RemoveUserOnline(session *Session) error
	IsUserOnline(session *Session) (bool, error)

	GetAllOnline(cc *Copy, session *Session) error

	//UserStatusChanged 通知基它服务有变化
	UserStatusChanged(status *Status, option *Option)
}

type UserList

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

type UserLog added in v0.1.22

type UserLog struct {
	Session Session
	TimeAt  int64
	Client  *Client
}

UserLog 用户,登录/登出

type UserStatus added in v0.1.22

type UserStatus struct {
	Session Session
	Addr    string
	TraceId string
	Status  int32
	TimeAt  int64
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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