Files

45 lines
1.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# NixMsg Go SDK
模块路径:`git.asio.asia/nixevol/NixMsg/sdk/go`,导入包名 `nixmsg`。
## 安装
```bash
# 公共代理访问不到 git.asio.asia 时:
# go env -w GOPRIVATE=git.asio.asia
go get git.asio.asia/nixevol/NixMsg/sdk/go@v0.1.0
```
版本标签带目录前缀:`sdk/go/v0.1.0`(由发布阶段打标,开发期可直接 `replace` 到本地仓库)。
## 最小示例
见 [`example/minimal/main.go`](./example/minimal/main.go):
```go
c := nixmsg.New()
c.OnSession(func(tok string) { /* 应用自行保存 */ })
c.OnMessage(func(msg nixmsg.Message) error {
fmt.Println(msg.From, msg.Body.Data)
return nil
})
ctx := context.Background()
_ = c.Connect(ctx, "ws://127.0.0.1:7443/mqtt", "device-1",
nixmsg.Credential{Password: "secret"}, nixmsg.Options{})
delay := time.Duration(0)
_, _ = c.Send(ctx, nixmsg.Target{Kind: "endpoint", ID: "device-2"},
nixmsg.Body{Enc: "utf8", Data: "hello"}, nixmsg.SendOptions{Delay: &delay})
_ = c.Close()
```
静态注册:
```go
res, err := nixmsg.Register(ctx, "ws://127.0.0.1:7443/mqtt", "reg-code",
nixmsg.RegisterOptions{ID: "device-1", LoginPassword: "secret", Name: "门口"})
```
## 许可证
见 `LICENSE`(专有)。