Parcourir la source

add host to string

Joyit il y a 1 mois
Parent
commit
7309514a27
2 fichiers modifiés avec 26 ajouts et 1 suppressions
  1. 1 1
      README.md
  2. 25 0
      type.go

+ 1 - 1
README.md

@@ -1,6 +1,6 @@
 # 一款简单的可以跨平台 mq 架构设计
 
-采用多对多的结构,可能方便切换不同的连接方式和不同的连接地址。
+提供服务端和客户端代码,采用多对多的结构,可能方便切换不同的连接方式和不同的连接地址。
 
 ## 设计原则
 

+ 25 - 0
type.go

@@ -1,8 +1,10 @@
 package tinymq
 
 import (
+	"bytes"
 	"fmt"
 	"regexp"
+	"strings"
 	"time"
 )
 
@@ -111,6 +113,29 @@ type HostInfo struct {
 	Updated time.Time `json:"updated" yaml:"updated"` // 节点信息刷新时间
 }
 
+// 只输出客户端要连接的信息
+func (h *HostInfo) String() string {
+	var b bytes.Buffer
+	b.WriteString(fmt.Sprintf("%s%d://", h.Proto, h.Version))
+	if h.Hash != "" {
+		b.WriteString(h.Hash + "@")
+	}
+	if strings.Contains(h.Host, ":") {
+		// ipv6
+		b.WriteString("[" + h.Host + "]")
+	} else {
+		b.WriteString(h.Host)
+	}
+	b.WriteString(fmt.Sprintf(":%d", h.Port))
+	if h.Path != "" {
+		b.WriteString(h.Path)
+	}
+	if h.Proxy {
+		b.WriteString("?proxy=1")
+	}
+	return b.String()
+}
+
 // 获取对应频道的一个连接地址
 type ConnectHostFunc func(channel string) (hostInfo *HostInfo, err error)