新增 zhub 客户端完整实现,包括配置加载、TCP通信、消息发布/订阅、 RPC 调用、分布式锁等功能。支持从项目配置文件、环境变量等多种方式初始化客户端,并提供便捷的全局调用接口。- 添加 `.gitignore` 忽略 IDE 和临时文件 - 实现 `api.go` 提供全局便捷方法封装客户端调用 - 实现 `client.go` 核心客户端逻辑,包含网络通信、消息处理等 - 添加 `client_test.go` 单元测试和集成测试示例 - 实现 `config.go` 支持灵活的配置加载机制 - 添加示例配置文件 `example-config.yml` - 初始化 Go 模块依赖 `go.mod` 和 `go.sum` - 实现 `init.go` 提供多种初始化方式 - 添加 MIT 许可证文件 `LICENSE` - 新增使用示例 `example/main.go` 展示基本功能调用 - 实现 `manager.go` 管理默认客户端实例及初始化逻辑
59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package zhub
|
|
|
|
import "time"
|
|
|
|
// 全局便捷方法
|
|
func Publish(topic, message string) error {
|
|
return GetClient().Publish(topic, message)
|
|
}
|
|
|
|
func Broadcast(topic, message string) error {
|
|
return GetClient().Broadcast(topic, message)
|
|
}
|
|
|
|
func Delay(topic, message string, delay int) error {
|
|
return GetClient().Delay(topic, message, delay)
|
|
}
|
|
|
|
func Subscribe(topic string, callback func(string)) {
|
|
GetClient().Subscribe(topic, callback)
|
|
}
|
|
|
|
func Unsubscribe(topic string) {
|
|
GetClient().Unsubscribe(topic)
|
|
}
|
|
|
|
func Timer(topic string, callback func()) {
|
|
GetClient().Timer(topic, callback)
|
|
}
|
|
|
|
func CallRpc(topic string, message interface{}, callback func(RpcResult)) {
|
|
GetClient().Rpc(topic, message, callback)
|
|
}
|
|
|
|
func RpcWithTimeout(topic string, message interface{}, timeout time.Duration, callback func(RpcResult)) {
|
|
GetClient().RpcWithTimeout(topic, message, timeout, callback)
|
|
}
|
|
|
|
func RpcSubscribe(topic string, handler func(Rpc) RpcResult) {
|
|
GetClient().RpcSubscribe(topic, handler)
|
|
}
|
|
|
|
func AcquireLock(key string, duration int) Lock {
|
|
return GetClient().Lock(key, duration)
|
|
}
|
|
|
|
func ReleaseLock(lock Lock) {
|
|
GetClient().Unlock(lock)
|
|
}
|
|
|
|
func Cmd(cmd ...string) {
|
|
GetClient().Cmd(cmd...)
|
|
}
|
|
|
|
func Close() {
|
|
if defaultClient != nil {
|
|
defaultClient.Close()
|
|
}
|
|
}
|