Golang中通道的Emitter Key生成方法

Golang中通道的Emitter Key生成方法 我需要使用 emitter 从后端生成一个频道密钥。

// 创建客户端并连接到代理
    c, _ := emitter.Connect(host, func(_ *emitter.Client, msg emitter.Message) {
        fmt.Printf("[emitter] -> [B] received: '%s' topic: '%s'\n", msg.Payload(), msg.Topic())
    })

    key2, err := c.GenerateKey(masterKey,newChannel, "rwls", ttl)
    if err != nil {
        panic(err)
    }

    fmt.Println(key2)

我没有收到任何错误,但也没有收到任何消息。知道这里出了什么问题吗?


更多关于Golang中通道的Emitter Key生成方法的实战教程也可以访问 https://www.itying.com/category-94-b0.html

9 回复

什么是“emitter”库?

更多关于Golang中通道的Emitter Key生成方法的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


c 是什么?有什么问题吗?代码是否发生恐慌?

我没有收到任何错误,但也没有收到任何消息。有人知道这里出了什么问题吗?

那么这一行执行到了吗?

panic(err)

这个 panic 有输出任何信息吗?错误 err 是什么?

‘c’ 是发射器代理

c, _ := emitter.Connect(host, func(_ *emitter.Client, msg emitter.Message) {
fmt.Printf("[emitter] -> [B] received: ‘%s’ topic: ‘%s’\n", msg.Payload(), msg.Topic())
})

问题是,代码没有进入 panic 块,甚至没有执行到下一行。

这个 panic 没有产生任何输出。

// Create the client and connect to the broker
c, _ := emitter.Connect(host, func(_ *emitter.Client, msg emitter.Message) {
	fmt.Printf("[emitter] -> [B] received: '%s' topic: '%s'\n", msg.Payload(), msg.Topic())
})

_, err := c.GenerateKey(masterKey,newChannel, "r", ttl)
if err != nil {
	panic(err)
}

emitter-io 库中存在一个问题,现已修复。

Pull Request 修复 request() 和 onMessage() 中的死锁问题 emitter-io:masterlzh2nix:fix-deadlock-when-waitcallback-and-OnMessage 于 2020年8月29日 00:59:26 UTC 开启 lzh2nix +3 -2

在Go语言中使用emitter库生成频道密钥时,如果代码没有报错但收不到消息,通常有几个常见原因需要排查。以下是可能的问题和解决方案:

1. 检查连接状态和订阅

确保连接成功后正确订阅了频道:

c, err := emitter.Connect(host, func(_ *emitter.Client, msg emitter.Message) {
    fmt.Printf("[emitter] -> [B] received: '%s' topic: '%s'\n", msg.Payload(), msg.Topic())
})
if err != nil {
    panic(err)
}

// 等待连接建立
time.Sleep(1 * time.Second)

// 生成密钥
key2, err := c.GenerateKey(masterKey, newChannel, "rwls", ttl)
if err != nil {
    panic(err)
}
fmt.Printf("Generated key: %s\n", key2)

// 使用生成的密钥订阅频道
err = c.Subscribe(key2, newChannel, func(_ *emitter.Client, msg emitter.Message) {
    fmt.Printf("Received message: %s on topic: %s\n", msg.Payload(), msg.Topic())
})
if err != nil {
    panic(err)
}

2. 验证密钥权限

确保生成的密钥具有正确的权限。权限字符串"rwls"的含义:

  • r:读取权限
  • w:写入权限
  • l:加载权限
  • s:存储权限

如果需要双向通信,应该同时具备读写权限:

// 生成具有读写权限的密钥
key2, err := c.GenerateKey(masterKey, newChannel, "rw", ttl)
if err != nil {
    panic(err)
}

3. 完整的发布-订阅示例

这里是一个完整的工作示例:

package main

import (
    "fmt"
    "time"
    "github.com/emitter-io/go"
)

func main() {
    host := "tcp://localhost:8080"
    masterKey := "your-master-key"
    channel := "test-channel/"
    ttl := 3600 // 1小时

    // 连接到emitter服务器
    c, err := emitter.Connect(host, func(client *emitter.Client, msg emitter.Message) {
        fmt.Printf("Control message: %s\n", msg.Payload())
    })
    if err != nil {
        panic(err)
    }
    defer c.Disconnect()

    // 等待连接建立
    time.Sleep(2 * time.Second)

    // 生成频道密钥
    channelKey, err := c.GenerateKey(masterKey, channel, "rwls", ttl)
    if err != nil {
        panic(err)
    }
    fmt.Printf("Generated channel key: %s\n", channelKey)

    // 订阅频道
    err = c.Subscribe(channelKey, channel, func(client *emitter.Client, msg emitter.Message) {
        fmt.Printf("Received: %s on topic: %s\n", msg.Payload(), msg.Topic())
    })
    if err != nil {
        panic(err)
    }

    // 发布测试消息
    err = c.Publish(channelKey, channel, "Hello from Go!")
    if err != nil {
        panic(err)
    }

    // 保持连接以接收消息
    time.Sleep(5 * time.Second)
}

4. 检查网络连接和服务器状态

添加连接状态监控:

c, err := emitter.Connect(host, func(client *emitter.Client, msg emitter.Message) {
    fmt.Printf("Message: %s\n", msg.Payload())
})

// 检查连接状态
if c.IsConnected() {
    fmt.Println("Successfully connected to emitter server")
} else {
    fmt.Println("Connection failed")
}

// 添加连接状态变化监听
c.OnConnect(func(client *emitter.Client) {
    fmt.Println("Connected to emitter server")
})

c.OnDisconnect(func(client *emitter.Client, err error) {
    fmt.Printf("Disconnected: %v\n", err)
})

5. 调试建议

添加详细的日志记录来诊断问题:

// 启用调试模式
emitter.SetDebug(true)

// 或者在连接时指定选项
c, err := emitter.Connect(host, func(client *emitter.Client, msg emitter.Message) {
    fmt.Printf("[%s] Topic: %s, Payload: %s\n", 
        time.Now().Format("15:04:05"), 
        msg.Topic(), 
        msg.Payload())
}, emitter.WithDebug(true))

主要检查点:

  1. 确保emitter服务器正在运行且可访问
  2. 验证master key有生成子密钥的权限
  3. 检查频道名称格式是否正确(通常以斜杠结尾)
  4. 确认订阅和发布使用相同的密钥和频道名称
  5. 检查TTL值是否有效(密钥可能已过期)
回到顶部