Joyit 1 ay önce
ebeveyn
işleme
aa3d01cd65
4 değiştirilmiş dosya ile 52 ekleme ve 52 silme
  1. 19 19
      examples/client-tcp2.go
  2. 19 19
      examples/client-ws2.go
  3. 11 13
      examples/server.go
  4. 3 1
      hub.go

+ 19 - 19
examples/client-tcp2.go

@@ -24,25 +24,25 @@ func main() {
 		Hash:    "xor:1qaz2wsx3",
 	}
 
-	hub := tinymq.NewHub(cf, localChannel, func(channel string, hostType tinymq.HostType) (hostInfo *tinymq.HostInfo, err error) {
-		return host, nil
-	}, func(client bool, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
-		log.Println("[AuthFunc]", client, proto, version, channel, string(remoteAuth))
-		return []byte("tinymq-client")
-		// 从 remoteAuth 是否为空来判断是否需要返回信息
-		// if len(remoteAuth) <= 0 {
-		// 	// 客户端调用,返回验证信息
-		// 	return []byte("tinymq")
-		// } else {
-		// 	// 服务端调用,返回验证token,或者其他信息
-		// 	return nil
-		// }
-	}, func(client bool, proto string, version uint8, channel string, auth []byte) bool {
-		log.Println("[CheckAuthFunc]", client, proto, version, channel, string(auth))
-		return string(auth) == "tinymq-server"
-	}, func(conn *tinymq.Line) {
-		log.Println("connect state", conn.Channel(), conn.State(), time.Since(conn.Updated()))
-	}, nil)
+	hub := tinymq.NewHub(
+		cf,
+		localChannel,
+		func(channel string, hostType tinymq.HostType) (hostInfo *tinymq.HostInfo, err error) {
+			return host, nil
+		},
+		func(client bool, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
+			log.Println("[AuthFunc]", client, proto, version, channel, string(remoteAuth))
+			return []byte("tinymq-client")
+		},
+		func(client bool, proto string, version uint8, channel string, auth []byte) bool {
+			log.Println("[CheckAuthFunc]", client, proto, version, channel, string(auth))
+			return string(auth) == "tinymq-server"
+		},
+		func(conn *tinymq.Line) {
+			log.Println("connect state", conn.Channel(), conn.State(), time.Since(conn.Updated()))
+		},
+		nil,
+	)
 
 	// 订阅频道
 	hub.Subscribe(remoteFilter, "hello", func(request *tinymq.RequestData) (state uint8, result any) {

+ 19 - 19
examples/client-ws2.go

@@ -27,25 +27,25 @@ func main() {
 		Hash: "xor:1qaz2wsx3edc",
 	}
 
-	hub := tinymq.NewHub(cf, localChannel, func(channel string, hostType tinymq.HostType) (hostInfo *tinymq.HostInfo, err error) {
-		return host, nil
-	}, func(client bool, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
-		log.Println("[AuthFunc]", client, proto, version, channel, string(remoteAuth))
-		return []byte("tinymq-client")
-		// 从 remoteAuth 是否为空来判断是否需要返回信息
-		// if len(remoteAuth) <= 0 {
-		// 	// 客户端调用,返回验证信息
-		// 	return []byte("tinymq")
-		// } else {
-		// 	// 服务端调用,返回验证token,或者其他信息
-		// 	return nil
-		// }
-	}, func(client bool, proto string, version uint8, channel string, auth []byte) bool {
-		log.Println("[CheckAuthFunc]", client, proto, version, channel, string(auth))
-		return string(auth) == "tinymq-server"
-	}, func(conn *tinymq.Line) {
-		log.Println("connect state", conn.Channel(), conn.State(), time.Since(conn.Updated()))
-	}, nil)
+	hub := tinymq.NewHub(
+		cf,
+		localChannel,
+		func(channel string, hostType tinymq.HostType) (hostInfo *tinymq.HostInfo, err error) {
+			return host, nil
+		},
+		func(client bool, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
+			log.Println("[AuthFunc]", client, proto, version, channel, string(remoteAuth))
+			return []byte("tinymq-client")
+		},
+		func(client bool, proto string, version uint8, channel string, auth []byte) bool {
+			log.Println("[CheckAuthFunc]", client, proto, version, channel, string(auth))
+			return string(auth) == "tinymq-server"
+		},
+		func(conn *tinymq.Line) {
+			log.Println("connect state", conn.Channel(), conn.State(), time.Since(conn.Updated()))
+		},
+		nil,
+	)
 
 	// 订阅频道
 	hub.Subscribe(remoteFilter, "hello", func(request *tinymq.RequestData) (state uint8, result any) {

+ 11 - 13
examples/server.go

@@ -22,22 +22,19 @@ func main() {
 
 	var hub *tinymq.Hub
 
-	hub = tinymq.NewHub(cf, localChannel,
-		nil, func(client bool, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
+	hub = tinymq.NewHub(
+		cf,
+		localChannel,
+		nil,
+		func(client bool, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
 			log.Println("[AuthFunc]", client, proto, version, channel, string(remoteAuth))
 			return []byte("tinymq-server")
-			// // 从 remoteAuth 是否为空来判断是否需要返回信息
-			// if len(remoteAuth) <= 0 {
-			// 	// 客户端调用,返回验证信息
-			// 	return []byte("tinymq")
-			// } else {
-			// 	// 服务端调用,返回验证token,或者其他信息
-			// 	return nil
-			// }
-		}, func(client bool, proto string, version uint8, channel string, auth []byte) bool {
+		},
+		func(client bool, proto string, version uint8, channel string, auth []byte) bool {
 			log.Println("[CheckAuthFunc]", client, proto, version, channel, string(auth))
 			return string(auth) == "tinymq-client"
-		}, func(conn *tinymq.Line) {
+		},
+		func(conn *tinymq.Line) {
 			log.Println("[Connect state change]", conn.Channel(), conn.State(), time.Since(conn.Updated()))
 			if conn.State() == tinymq.Connected {
 				go hub.Get(remoteFilter, "hello", []byte("hello from server push"),
@@ -51,7 +48,8 @@ func main() {
 						return true
 					})
 			}
-		}, nil,
+		},
+		nil,
 	)
 
 	// tcp2协议

+ 3 - 1
hub.go

@@ -589,6 +589,8 @@ func (h *Hub) BindForServer(info *HostInfo) (err error) {
 			return
 		}
 		if !h.checkAuthFunc(false, proto, version, channel, auth) {
+			err = fmt.Errorf("[server checkAuthFunc ERROR] in proto: %s, version: %d, channel: %s, auth: %s", proto, version, channel, string(auth))
+			log.Println(err)
 			conn.Close()
 			return
 		}
@@ -740,7 +742,7 @@ func (h *Hub) ConnectToServer(channel string, force bool, host *HostInfo) (err e
 	}
 	// 检查验证是否合法
 	if !h.checkAuthFunc(true, proto, version, channel, auth) {
-		err = fmt.Errorf("[checkAuthFunc ERROR] in proto: %s, version: %d, channel: %s, auth: %s", proto, version, channel, string(auth))
+		err = fmt.Errorf("[client checkAuthFunc ERROR] in proto: %s, version: %d, channel: %s, auth: %s", proto, version, channel, string(auth))
 		log.Println(err)
 		conn.Close()
 		host.Errors++