notice

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2025 License: AGPL-3.0 Imports: 15 Imported by: 18

Documentation

Index

Constants

View Source
const (
	ModeElement = `element`
	ModeNotify  = `notify`
	ModeModal   = `modal`
	ModeDash    = `-`
)
View Source
const (
	Succeed         = 1
	Failed          = 0
	Unauthenticated = -1
	Forbid          = -2
)
View Source
const (
	StateSuccess = 1
	StateFailure = 0
)
View Source
const AnyClientID = `any`
View Source
const SSEventName = `notice`

Variables

View Source
var (
	ErrUserNotOnline     = errors.New(`通知错误:用户不在线`)
	ErrMsgTypeNotAccept  = errors.New(`通知错误:不接受这种消息类型`)
	ErrClientIDNotOnline = errors.New(`通知错误:ClientID不在线`)
	ErrForcedExit        = errors.New(`Forced exit`)
)
View Source
var (
	// DefaultNoticer 默认noticer
	// state > 0 为成功;否则为失败
	DefaultNoticer Noticer = func(message interface{}, state int, progs ...*Progress) error {
		if len(progs) > 0 && progs[0] != nil {
			message = `[ ` + tplfunc.NumberFormat(progs[0].CalcPercent().Percent(), 2) + `% ] ` + fmt.Sprint(message)
		}
		if state > 0 {
			log.Info(message)
		} else {
			log.Error(message)
		}
		return nil
	}

	CustomOutputNoticer CustomWithWriter = func(wOut io.Writer, wErr io.Writer) Noticer {
		return func(message interface{}, state int, progs ...*Progress) error {
			if len(progs) > 0 && progs[0] != nil {
				message = `[ ` + tplfunc.NumberFormat(progs[0].CalcPercent().Percent(), 2) + `% ] ` + fmt.Sprint(message)
			}
			if state > 0 {
				fmt.Fprintln(wOut, message)
			} else {
				fmt.Fprintln(wErr, message)
			}
			return nil
		}
	}
)
View Source
var NoticeMessageChanSize = 3
View Source
var SSERender = &sseRender{
	ServerSentEvents: sseRenderUtils.New(),
}

Functions

func Clear

func Clear()

func CloseClient

func CloseClient(user string, clientID string) bool

func CloseMessage

func CloseMessage(user string, types ...string)

func DownloadProxyFn

func DownloadProxyFn(np NProgressor) func(name string, download int, size int64, r io.Reader) io.Reader

func GetClientID

func GetClientID(eCtx echo.Context) string

func GetNoticeID

func GetNoticeID(eCtx echo.Context) string

func GetNoticeMode

func GetNoticeMode(eCtx echo.Context) string

func Initialize

func Initialize()

func IsOnline

func IsOnline(user string) bool

func NewUserNotices

func NewUserNotices(debug bool, users IOnlineUsers) *userNotices

func OnlineStatus

func OnlineStatus(users ...string) map[string]bool

func OnlineUserCount added in v0.5.12

func OnlineUserCount() int

func OnlineUserList added in v0.5.12

func OnlineUserList(limit int) []string

func OpenMessage

func OpenMessage(user string, types ...string)

func Recv

func Recv(user string, clientID string) <-chan *Message

func Send

func Send(user string, message *Message) error

func SetAutoComplete added in v0.12.4

func SetAutoComplete(autoComplete bool) func(*Config)

func SetClientID

func SetClientID(clientID string) func(*Config)

func SetDebug

func SetDebug(on bool)

func SetID

func SetID(id interface{}) func(*Config)

func SetIsExited

func SetIsExited(isExited IsExited) func(*Config)

func SetMode

func SetMode(mode string) func(*Config)

func SetTimeout

func SetTimeout(timeout time.Duration) func(*Config)

func SetType

func SetType(typ string) func(*Config)

func SetUser

func SetUser(user string) func(*Config)

func Stdout

func Stdout(message *Message)

func ToReadCloser

func ToReadCloser(r io.Reader) io.ReadCloser

func ToWriteCloser

func ToWriteCloser(w io.Writer) io.WriteCloser

Types

type Callback added in v0.8.7

type Callback struct {
	Success func(*Message)
	Failure func(*Message)
}

type Config added in v0.12.4

type Config struct {
	User         string
	Type         string // Topic
	ClientID     string
	ID           interface{}
	IsExited     IsExited
	Timeout      time.Duration
	Mode         string // element / notify
	AutoComplete bool   // 是否自动完成
	// contains filtered or unexported fields
}

func NewConfig added in v0.12.4

func NewConfig() *Config

func NewConfigByContext added in v0.12.4

func NewConfigByContext(eCtx echo.Context, noticeType string, user string, opts ...func(*Config)) *Config

func (*Config) Noticer added in v0.12.4

func (c *Config) Noticer(ctx context.Context) Noticer

func (*Config) Progress added in v0.12.4

func (c *Config) Progress() *Progress

func (*Config) SetAutoComplete added in v0.12.4

func (c *Config) SetAutoComplete(autoComplete bool) *Config

func (*Config) SetClientID added in v0.12.4

func (c *Config) SetClientID(clientID string) *Config

func (*Config) SetID added in v0.12.4

func (c *Config) SetID(id interface{}) *Config

func (*Config) SetIsExited added in v0.12.4

func (c *Config) SetIsExited(isExited IsExited) *Config

func (*Config) SetMode added in v0.12.4

func (c *Config) SetMode(mode string) *Config

func (*Config) SetTimeout added in v0.12.4

func (c *Config) SetTimeout(t time.Duration) *Config

func (*Config) SetType added in v0.12.4

func (c *Config) SetType(typ string) *Config

func (*Config) SetUser added in v0.12.4

func (c *Config) SetUser(user string) *Config

type Control

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

func (*Control) Exited

func (c *Control) Exited() *Control

func (*Control) IsExited

func (c *Control) IsExited() bool

func (*Control) ListenContextAndTimeout

func (c *Control) ListenContextAndTimeout(ctx context.Context, timeouts ...time.Duration) *Control

type CustomWithWriter

type CustomWithWriter func(wOut io.Writer, wErr io.Writer) Noticer

type IOnlineUser

type IOnlineUser interface {
	GetUser() string
	HasMessageType(messageTypes ...string) bool
	Send(message *Message, openDebug ...bool) error
	Recv(clientID string) <-chan *Message
	ClearMessage()
	ClearMessageType(types ...string)
	OpenMessageType(types ...string)
	CountType() int
	CountClient() int
	CloseClient(clientID string) int
	OpenClient(clientID string)
}

func OpenClient

func OpenClient(user string) (oUser IOnlineUser, clientID string)

func OpenClientWithID added in v0.12.1

func OpenClientWithID(user string, clientID string) IOnlineUser

type IOnlineUsers

type IOnlineUsers interface {
	GetOk(user string, noLock ...bool) (IOnlineUser, bool)
	OnlineStatus(users ...string) map[string]bool
	IsOnline(user string) bool
	Set(user string, oUser IOnlineUser)
	Delete(user string)
	Clear()
	Count() int
	UserList(limit int) []string
}

type IsExited

type IsExited interface {
	IsExited() bool
}

func NewControlWithContext

func NewControlWithContext(ctx context.Context, timeout time.Duration) IsExited

type Message

type Message struct {
	ClientID string        `json:"client_id" xml:"client_id"`
	ID       interface{}   `json:"id" xml:"id"`
	Type     string        `json:"type" xml:"type"`
	Title    string        `json:"title" xml:"title"`
	Status   int           `json:"status" xml:"status"`
	Content  interface{}   `json:"content" xml:"content"`
	Mode     string        `json:"mode" xml:"mode"` //显示模式:notify/element/modal
	Progress *ProgressInfo `json:"progress" xml:"progress"`
	// contains filtered or unexported fields
}

func NewMessage

func NewMessage() *Message

func NewMessageWithValue

func NewMessageWithValue(typ string, title string, content interface{}, status ...int) *Message

func (*Message) Failure added in v0.8.7

func (m *Message) Failure()

func (*Message) Release

func (m *Message) Release()

func (*Message) SetCallback added in v0.8.7

func (m *Message) SetCallback(cb *Callback) *Message

func (*Message) SetClientID

func (m *Message) SetClientID(clientID string) *Message

func (*Message) SetContent

func (m *Message) SetContent(content interface{}) *Message

func (*Message) SetID

func (m *Message) SetID(id interface{}) *Message

func (*Message) SetMode

func (m *Message) SetMode(mode string) *Message

func (*Message) SetProgress

func (m *Message) SetProgress(progress *Progress) *Message

func (*Message) SetStatus

func (m *Message) SetStatus(status int) *Message

func (*Message) SetTitle

func (m *Message) SetTitle(title string) *Message

func (*Message) SetType

func (m *Message) SetType(t string) *Message

func (*Message) Success added in v0.8.7

func (m *Message) Success()

type MessageWithStatus

type MessageWithStatus struct {
	Message interface{}
	Status  int
}

type NProgressor

type NProgressor interface {
	Notifier
	Progressor
}

type Notice

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

func NewNotice

func NewNotice() *Notice

func (*Notice) CloseClient

func (a *Notice) CloseClient(clientID string) int

func (*Notice) CountClient

func (a *Notice) CountClient() int

func (*Notice) OpenClient

func (a *Notice) OpenClient(clientID string)

type NoticeAndProgress

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

func NewP

func NewP(eCtx echo.Context, noticeType string, user string, ctx context.Context, opts ...func(*Config)) *NoticeAndProgress

func NewWithProgress

func NewWithProgress(noticer Noticer, progresses ...*Progress) *NoticeAndProgress

func (*NoticeAndProgress) Add

func (*NoticeAndProgress) AutoComplete

func (a *NoticeAndProgress) AutoComplete(on bool) NProgressor

func (*NoticeAndProgress) Callback

func (a *NoticeAndProgress) Callback(total int64, exec func(callback func(strLen int)) error) error

func (*NoticeAndProgress) Complete

func (a *NoticeAndProgress) Complete() NProgressor

func (*NoticeAndProgress) Done

func (a *NoticeAndProgress) Done(n int64) NProgressor

func (*NoticeAndProgress) Failure

func (a *NoticeAndProgress) Failure(message interface{}) error

func (*NoticeAndProgress) OnlyAdd added in v0.13.1

func (a *NoticeAndProgress) OnlyAdd(n int64) NProgressor

func (*NoticeAndProgress) Progress added in v0.9.15

func (a *NoticeAndProgress) Progress() *Progress

func (*NoticeAndProgress) ProxyReader

func (p *NoticeAndProgress) ProxyReader(r io.Reader) io.ReadCloser

func (*NoticeAndProgress) ProxyWriter

func (p *NoticeAndProgress) ProxyWriter(w io.Writer) io.WriteCloser

func (*NoticeAndProgress) Reset

func (a *NoticeAndProgress) Reset()

func (*NoticeAndProgress) Send

func (a *NoticeAndProgress) Send(message interface{}, statusCode int) error

func (*NoticeAndProgress) SetControl added in v0.9.15

func (a *NoticeAndProgress) SetControl(control IsExited) NProgressor

func (*NoticeAndProgress) Success

func (a *NoticeAndProgress) Success(message interface{}) error

type NoticeMessager

type NoticeMessager interface {
	Size() int
	Delete(clientID string) int
	Clear()
	Add(clientID string)
	Send(message *Message) error
	Recv(clientID string) <-chan *Message
}

type NoticeTyper

type NoticeTyper interface {
	Has(types ...string) bool
	Size() int
	Clear(types ...string)
	Open(types ...string)
}

type Noticer

type Noticer func(message interface{}, state int, progress ...*Progress) error

func MakeNoticer

func MakeNoticer(progress *Progress, msgType string, mode string, id interface{}, clientID string, user string) Noticer

func New

func New(eCtx echo.Context, noticeType string, user string, ctx context.Context, opts ...func(*Config)) Noticer

func NewNoticer

func NewNoticer(ctx context.Context, config *Config) Noticer

func NewNoticerByConfig added in v0.12.4

func NewNoticerByConfig(ctx context.Context, config *Config) Noticer

func (Noticer) WithProgress

func (noticer Noticer) WithProgress(progresses ...*Progress) *NoticeAndProgress

type Notifier

type Notifier interface {
	Send(message interface{}, statusCode int) error
	Success(message interface{}) error
	Failure(message interface{}) error
}

type OnlineUser

type OnlineUser struct {
	User string
	*Notice
}

func NewOnlineUser

func NewOnlineUser(user string) *OnlineUser

func (*OnlineUser) ClearMessage

func (oUser *OnlineUser) ClearMessage()

func (*OnlineUser) ClearMessageType

func (oUser *OnlineUser) ClearMessageType(types ...string)

func (*OnlineUser) CountType

func (oUser *OnlineUser) CountType() int

func (*OnlineUser) GetUser

func (oUser *OnlineUser) GetUser() string

func (*OnlineUser) HasMessageType

func (oUser *OnlineUser) HasMessageType(messageTypes ...string) bool

func (*OnlineUser) OpenMessageType

func (oUser *OnlineUser) OpenMessageType(types ...string)

func (*OnlineUser) Recv

func (oUser *OnlineUser) Recv(clientID string) <-chan *Message

func (*OnlineUser) Send

func (oUser *OnlineUser) Send(message *Message, openDebug ...bool) error

type OnlineUsers

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

func NewOnlineUsers

func NewOnlineUsers() *OnlineUsers

func (*OnlineUsers) Clear

func (o *OnlineUsers) Clear()

func (*OnlineUsers) Count added in v0.5.12

func (o *OnlineUsers) Count() int

Count 在线用户人数

func (*OnlineUsers) Delete

func (o *OnlineUsers) Delete(user string)

func (*OnlineUsers) GetOk

func (o *OnlineUsers) GetOk(user string, noLock ...bool) (IOnlineUser, bool)

func (*OnlineUsers) IsOnline added in v0.6.1

func (o *OnlineUsers) IsOnline(user string) bool

func (*OnlineUsers) OnlineStatus

func (o *OnlineUsers) OnlineStatus(users ...string) map[string]bool

func (*OnlineUsers) Set

func (o *OnlineUsers) Set(user string, oUser IOnlineUser)

func (*OnlineUsers) UserList added in v0.5.12

func (o *OnlineUsers) UserList(limit int) []string

UserList 在线用户列表 limit=-1时为获取全部用户

type Progress

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

func NewProgress

func NewProgress(autoComplete ...bool) *Progress

func (*Progress) Add

func (p *Progress) Add(n int64) *Progress

func (*Progress) AutoComplete

func (p *Progress) AutoComplete(on bool) *Progress

func (*Progress) CalcPercent

func (p *Progress) CalcPercent() *Progress

func (*Progress) Callback

func (p *Progress) Callback(total int64, exec func(callback func(strLen int)) error) error

func (*Progress) CloneInfo

func (p *Progress) CloneInfo() ProgressInfo

func (*Progress) Complete

func (p *Progress) Complete() bool

func (*Progress) CopyToInfo

func (p *Progress) CopyToInfo(to *ProgressInfo)

func (*Progress) Done

func (p *Progress) Done(n int64) int64

func (*Progress) Finish

func (p *Progress) Finish() int64

func (*Progress) IsExited

func (p *Progress) IsExited() bool

func (*Progress) OnlyAdd added in v0.13.1

func (p *Progress) OnlyAdd(n int64) *Progress

func (*Progress) Percent

func (p *Progress) Percent() float64

func (*Progress) Reset

func (p *Progress) Reset()

func (*Progress) SetComplete

func (p *Progress) SetComplete() *Progress

func (*Progress) SetControl

func (p *Progress) SetControl(control IsExited) *Progress

func (*Progress) SetPercent

func (p *Progress) SetPercent(percent float64) *Progress

func (*Progress) Total

func (p *Progress) Total() int64

type ProgressInfo

type ProgressInfo struct {
	Total    int64   `json:"total" xml:"total"`
	Finish   int64   `json:"finish" xml:"finish"`
	Percent  float64 `json:"percent" xml:"percent"`
	Complete bool    `json:"complete" xml:"complete"`
}

type Progressor

type Progressor interface {
	Add(n int64) NProgressor
	OnlyAdd(n int64) NProgressor
	Done(n int64) NProgressor
	AutoComplete(on bool) NProgressor
	Complete() NProgressor
	Reset()
	ProxyReader(r io.Reader) io.ReadCloser
	ProxyWriter(w io.Writer) io.WriteCloser
	Callback(total int64, exec func(callback func(strLen int)) error) error
	SetControl(IsExited) NProgressor
	Progress() *Progress
}

type UserMessageSystem added in v0.6.1

type UserMessageSystem interface {
	SetDebug(on bool) UserMessageSystem
	Debug() bool
	OnClose(fn ...func(user string)) UserMessageSystem
	OnOpen(fn ...func(user string)) UserMessageSystem
	Sendable(user string, types ...string) bool
	Send(user string, message *Message) error
	Recv(user string, clientID string) <-chan *Message
	CloseClient(user string, clientID string) bool
	IsOnline(user string) bool
	OnlineStatus(users ...string) map[string]bool
	OpenClient(user string) (oUser IOnlineUser, clientID string)
	OpenClientWithID(user string, clientID string) (oUser IOnlineUser)
	CloseMessage(user string, types ...string)
	OpenMessage(user string, types ...string)
	Clear()
	Count() int
	UserList(limit int) []string
	MakeMessageGetter(username string, messageTypes ...string) (func(), <-chan *Message, error)
	MakeMessageGetterWithClientID(username string, clientID string, messageTypes ...string) (func(), <-chan *Message)
}

func Default

func Default() UserMessageSystem

func OnClose

func OnClose(fn ...func(user string)) UserMessageSystem

func OnOpen

func OnOpen(fn ...func(user string)) UserMessageSystem

Jump to

Keyboard shortcuts

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