亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Go 中接收 Redis 發布消息

如何在 Go 中接收 Redis 發布消息

Go
絕地無雙 2022-09-26 15:18:52
我正在嘗試在Go中使用Redis PubSub,以便能夠傳遞/發布消息并在訂閱期間檢索它。我已經能夠正確設置代碼的發布和訂閱/ PubSub部分。以下是我的代碼。我希望在訂閱期間檢索的(字符串)消息是 但是,我的代碼的輸出給出了通道,種類和計數,并且不顯示預期的消息()。test message.test message使用 Redis 在 Go 中發布/訂閱發布后,如何獲取預期的消息(測試消息)?我覺得我很親近,但我可能在這里錯過了一件小事。我對雷迪斯很陌生。感謝您的幫助。以下是我的代碼:package mainimport (    "fmt"    "log"    "time"    "github.com/gomodule/redigo/redis")func main() {    fmt.Println("Start redis test.")    c, err := redis.Dial("tcp", "localhost:6379")    if err != nil {        log.Println(err)    } else {        log.Println("No error during redis.Dial.")    }    // defer c.Close()    /// Publisher.    c.Do("PUBLISH", "example", "test message")    /// End here    /// Subscriber.    psc := redis.PubSubConn{Conn: c}    psc.Subscribe("example")    for {        switch v := psc.Receive().(type) {        case redis.Message:            fmt.Printf("%s: message: %s\n", v.Channel, v.Data)        case redis.Subscription:            fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count)        case error:            fmt.Println(v)        }    }    /// End here}以下是我的輸出:example: subscribe 1
查看完整描述

1 回答

?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

我相信你的代碼很好;問題是您在訂閱處于活動狀態之前發布消息。例如,試試這個,它把你的發布者放到一個每秒發布一條消息的 goroutine 中:


package main


import (

    "fmt"

    "log"

    "time"


    "github.com/gomodule/redigo/redis"

)


func main() {

    fmt.Println("Start redis test.")


    c, err := redis.Dial("tcp", "localhost:6379")

    if err != nil {

        log.Println(err)

    } else {

        log.Println("No error during redis.Dial.")

    }

    // defer c.Close()


    /// Publisher.

    go func() {

        c, err := redis.Dial("tcp", "localhost:6379")

        if err != nil {

            panic(err)

        }


        count := 0

        for {

            c.Do("PUBLISH", "example",

                fmt.Sprintf("test message %d", count))

            count++

            time.Sleep(1 * time.Second)

        }

    }()

    /// End here


    /// Subscriber.

    psc := redis.PubSubConn{Conn: c}

    psc.Subscribe("example")


    for {

        switch v := psc.Receive().(type) {

        case redis.Message:

            fmt.Printf("%s: message: %s\n", v.Channel, v.Data)

        case redis.Subscription:

            fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count)

        case error:

            fmt.Println(v)

        }


        time.Sleep(1)

    }

    /// End here


}

運行此命令,您將看到您的訂閱者每秒收到一條消息,產生如下輸出:


Start redis test.

2021/08/18 19:01:29 No error during redis.Dial.

example: subscribe 1

example: message: test message 0

example: message: test message 1

example: message: test message 2

example: message: test message 3

example: message: test message 4

example: message: test message 5


查看完整回答
反對 回復 2022-09-26
  • 1 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號