pushKit

package
v3.1.112 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2024 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package pushKit 此文件中的几个方法,需要在子类中实现!

Index

Constants

This section is empty.

Variables

View Source
var (
	PingData = []byte("ping")

	PongData = []byte("pong")
)
View Source
var (
	NotSetupError = errorKit.Newf("haven’t been set up correctly")

	ChannelClosedError = errorKit.Newf("channel has already been closed")

	NoSuitableChannelError = errorKit.Newf("no suitable channel")
)

Functions

func BindBsid

func BindBsid(channel Channel, bsid string)

func BindGroup

func BindGroup(channel Channel, group string)

func BindId

func BindId(channel Channel, id string)

func BindUser

func BindUser(channel Channel, user string)

func CheckSetup added in v3.0.38

func CheckSetup() error

func CloseAll

func CloseAll(reason string)

func CloseByBsid

func CloseByBsid(bsid string, reason string) (err error)

CloseByBsid

PS: 解绑(unbind)后续由 inner handler 处理.

func CloseByGroup

func CloseByGroup(group string, reason string)

CloseByGroup

PS: 解绑(unbind)后续由 inner handler 处理.

func CloseById

func CloseById(id string, reason string) (err error)

CloseById

PS: 解绑(unbind)后续由 inner handler 处理.

func CloseByUser

func CloseByUser(user string, reason string)

CloseByUser

PS: 解绑(unbind)后续由 inner handler 处理.

func DefaultIdGenerator

func DefaultIdGenerator() func() (string, error)

func GetCountOfBsidMap

func GetCountOfBsidMap() int

func GetCountOfGroupMap

func GetCountOfGroupMap() (count int)

func GetCountOfIdMap

func GetCountOfIdMap() int

func GetCountOfUserMap

func GetCountOfUserMap() (count int)

func GetDefaultLogger

func GetDefaultLogger() *logrus.Logger

func GetStatistics

func GetStatistics() string

GetStatistics

e.g. +----------+-------------------+ | TYPE | COUNT OF CHANNELS | +----------+-------------------+ | idMap | 0 | | bsidMap | 0 | | userMap | 0 | | groupMap | 0 | +----------+-------------------+

func IsNoSuitableChannelError added in v3.0.52

func IsNoSuitableChannelError(err error) bool

IsNoSuitableChannelError 推送返回的error,是否是因为不存在对应的channel?

func MustSetUp

func MustSetUp(antPool *ants.Pool, logger *logrus.Logger)

func PushToAll

func PushToAll(data []byte, exceptBsids []string) (err error)

func PushToBsid

func PushToBsid(data []byte, bsid string) (err error)

func PushToGroup

func PushToGroup(data []byte, group string, exceptBsids []string) (err error)

func PushToUser

func PushToUser(data []byte, user string, exceptBsids []string) (err error)

func SetDefaultLogger

func SetDefaultLogger(logrusLogger *logrus.Logger) error

func Setup

func Setup(antPool *ants.Pool, logger *logrus.Logger) (err error)

Setup

@param antPool 用来并发执行推送任务

(1) 不能为nil
(2) 需要自行决定: cap大小、是否自定义输出...

@param logger 可以为nil @param options

func UnbindBsid

func UnbindBsid(channel Channel)

UnbindBsid

PS: 仅是解绑,不会关闭channel.

func UnbindGroup

func UnbindGroup(channel Channel)

UnbindGroup

PS: (1) 仅是解绑,不会关闭channel; (2) 解绑成功后,如果set为空,应该移除掉.

func UnbindId

func UnbindId(channel Channel)

UnbindId

PS: 仅是解绑,不会关闭channel.

func UnbindUser

func UnbindUser(channel Channel)

UnbindUser

PS: (1) 仅是解绑,不会关闭channel; (2) 解绑成功后,如果set为空,应该移除掉.

Types

type BaseChannel

type BaseChannel struct {
	gmutex.RWMutex

	CloseCh chan string

	// ClientIP 可能是error string(获取失败的情况下).
	ClientIP string
	// Type Channel 的类型.
	Type string

	Id    string
	Bsid  string
	User  string
	Group string
	Data  interface{}

	Closed    bool
	Listeners Listeners

	PongInterval time.Duration
	Interval     *timeKit.Interval
}

BaseChannel

!!!: (1) 此类型实现了部分 Channel 接口, (2) 此类型的子类应当实现 Channel 接口(主要是: Push()、Close()、bind、unbind),不能覆盖父类的方法.

(由于unbind可能涉及Close(),因此只能在子类中实现)

func (*BaseChannel) ClearBsid

func (channel *BaseChannel) ClearBsid()

func (*BaseChannel) ClearData

func (channel *BaseChannel) ClearData()

func (*BaseChannel) ClearGroup

func (channel *BaseChannel) ClearGroup()

func (*BaseChannel) ClearUser

func (channel *BaseChannel) ClearUser()

func (*BaseChannel) Close added in v3.0.59

func (channel *BaseChannel) Close(reason string) error

func (*BaseChannel) Dispose added in v3.0.59

func (channel *BaseChannel) Dispose()

func (*BaseChannel) Equals

func (channel *BaseChannel) Equals(c Channel) bool

Equals 根据 id 判断是否相等.

func (*BaseChannel) GetBsid

func (channel *BaseChannel) GetBsid() (bsid string)

func (*BaseChannel) GetClientIP

func (channel *BaseChannel) GetClientIP() string

func (*BaseChannel) GetCloseCh

func (channel *BaseChannel) GetCloseCh() chan string

GetCloseCh

PS: 只有 调用SetClosed() && 返回值为true 的情况下,才能向通道中写数据.

func (*BaseChannel) GetData

func (channel *BaseChannel) GetData() (data interface{})

func (*BaseChannel) GetGroup

func (channel *BaseChannel) GetGroup() (group string)

func (*BaseChannel) GetId

func (channel *BaseChannel) GetId() (id string)

func (*BaseChannel) GetType

func (channel *BaseChannel) GetType() string

func (*BaseChannel) GetUser

func (channel *BaseChannel) GetUser() (user string)

func (*BaseChannel) Initialize added in v3.0.59

func (channel *BaseChannel) Initialize() error

func (*BaseChannel) IsClosed

func (channel *BaseChannel) IsClosed() (rst bool)

IsClosed (读锁)

func (*BaseChannel) Push added in v3.0.59

func (channel *BaseChannel) Push(data []byte) error

func (*BaseChannel) SetBsid

func (channel *BaseChannel) SetBsid(bsid string)

func (*BaseChannel) SetClosed

func (channel *BaseChannel) SetClosed() (flag bool)

SetClosed (写锁)

PS: 返回值如果为true,应当调用 Listeners.OnClose().

@return true: 设置成功

false:	设置失败(因为已经被设置关闭)

func (*BaseChannel) SetData

func (channel *BaseChannel) SetData(data interface{})

func (*BaseChannel) SetGroup

func (channel *BaseChannel) SetGroup(group string)

func (*BaseChannel) SetUser

func (channel *BaseChannel) SetUser(user string)

type Channel

type Channel interface {
	// Close 后端主动关闭通道.
	/*
		@param reason 关闭的原因
	*/
	Close(reason string) error
	IsClosed() (rst bool)
	// SetClosed
	/*
		PS: 返回值如果为true,应当 向closeCh发送数据 || 调用Listeners.OnClose().
	*/
	SetClosed() (flag bool)
	// GetCloseCh 获取关闭通道.
	/*
		PS: 只有 调用SetClosed() && 返回值为true 的情况下,才能向通道中写数据.
	*/
	GetCloseCh() chan string

	// Initialize 初始化Channel.
	Initialize() error

	// Dispose 释放Channel所持有的资源.
	Dispose()

	Push(data []byte) error

	GetClientIP() string
	GetType() string

	GetId() string
	GetBsid() string
	SetBsid(string)
	ClearBsid()
	GetUser() string
	SetUser(string)
	ClearUser()
	GetGroup() string
	SetGroup(string)
	ClearGroup()
	GetData() interface{}
	SetData(interface{})
	ClearData()

	Equals(c Channel) bool
}

type Listener

type Listener interface {
	OnFailure(w http.ResponseWriter, r *http.Request, failureInfo string)

	OnHandshake(w http.ResponseWriter, r *http.Request, channel Channel)

	// OnMessage 收到 客户端 发来的消息.
	/*
		PS: 仅适用于WebSocket连接,因为SSE连接是单工的.
	*/
	OnMessage(channel Channel, messageType int, data []byte)

	// BeforeClosedByBackend 由于后端主动关闭连接,而触发的关闭事件.
	BeforeClosedByBackend(channel Channel, closeInfo string)

	OnClose(channel Channel, closeInfo string, bsid, user, group string)
}

type Listeners

type Listeners []Listener

func NewListeners

func NewListeners(listener Listener, sseFlag bool) (Listeners, error)

NewListeners

PS: 本方法仅供本项目使用,严禁外部调用.

func (Listeners) BeforeClosedByBackend

func (listeners Listeners) BeforeClosedByBackend(channel Channel, closeInfo string)

func (Listeners) OnClose

func (listeners Listeners) OnClose(channel Channel, closeInfo string)

func (Listeners) OnFailure

func (listeners Listeners) OnFailure(w http.ResponseWriter, r *http.Request, failureInfo string)

func (Listeners) OnHandshake

func (listeners Listeners) OnHandshake(w http.ResponseWriter, r *http.Request, channel Channel)

func (Listeners) OnMessage

func (listeners Listeners) OnMessage(channel Channel, messageType int, data []byte)

type Processor

type Processor interface {
	ProcessWithGin(ctx *gin.Context)

	Process(w http.ResponseWriter, r *http.Request)
}

Jump to

Keyboard shortcuts

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