代码拉取完成,页面将自动刷新
package emq5
import (
"strings"
)
type Engine struct {
*Options
Topic string
TopicLen int
Wildcard bool
Shared bool
ShareGroup string
Qos int
}
func (o *Options) NewEngine(topic string, wildcard bool, qos int) (eng *Engine) {
eng = &Engine{
Options: o,
Topic: topic,
Wildcard: wildcard,
Qos: qos,
}
eng.checkTopic()
o.Engine = append(o.Engine, eng)
return
}
func (o *Options) NewSingleEngine(topic string, wildcard bool, qos int, handler Handler) {
eng := &Engine{
Options: o,
Topic: topic,
Qos: qos,
}
eng.checkTopic()
o.Engine = append(o.Engine, eng)
eng.Register("", handler)
return
}
func (e *Engine) checkTopic() {
if e.Topic[0] == '/' {
e.Logger.Error("topic禁止以'/'符号开头")
return
}
if e.Topic[len(e.Topic)-1] == '/' {
e.Logger.Error("topic禁止以'/'符号开头")
return
}
e.TopicLen = len(strings.Split(e.Topic, "/"))
}
// Register 注册方法到前缀树
func (e *Engine) Register(url string, handler Handler) {
e.checkUrl(url)
topic := e.Topic + url
if handler == nil {
e.Logger.Error("此topic未绑定方法")
return
}
path := e.splitUrl(topic)
for i, s := range path {
if s == "" {
path = append(path[:i], path[i+1:]...)
}
}
e.Options.node = e.Options.node.addRoute(path, handler)
}
// WithShare 注册共享订阅
func (e *Engine) WithShare(group string) {
e.Shared = true
e.ShareGroup = group
if group == "" {
e.ShareGroup = "default"
}
}
func (e *Engine) splitUrl(url string) []string {
urlList := strings.Split(url, "/")
for i, s := range urlList {
if s == "" {
urlList = append(urlList[:i], urlList[i+1:]...)
}
}
return urlList
}
func (e *Engine) checkUrl(url string) {
if url == "" {
return
}
if url[0] != '/' {
e.Logger.Error("路由必须以'/'符号开头")
return
}
if url[len(url)-1] == '/' {
e.Logger.Error("路由禁止以'/'符号开头")
return
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。