本文整理汇总了Golang中menteslibres/net/gosexy/redis.Client.Publish方法的典型用法代码示例。如果您正苦于以下问题:Golang Client.Publish方法的具体用法?Golang Client.Publish怎么用?Golang Client.Publish使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类menteslibres/net/gosexy/redis.Client
的用法示例。
在下文中一共展示了Client.Publish方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: processSerialData
func (s SerialReader) processSerialData(redisConnection *redis.Client) {
serialConnection := s.newSerialConnection()
reader := bufio.NewReader(serialConnection)
re := regexp.MustCompile("^([^:]+):(.+)$")
for {
reply, err := reader.ReadBytes('\n')
if err != nil {
log.Printf("processSerialData() #1: %s", err)
continue
}
data := strings.TrimSpace(string(reply))
if len(data) == 0 {
continue
}
log.Printf("%s", data)
raw_data := re.FindStringSubmatch(data)
if len(raw_data) == 0 {
continue
}
metrics := strings.Split(raw_data[2], ",")
channel := fmt.Sprintf("rpi-moteino-collector:%s", raw_data[1])
for i := 0; i < len(metrics); i++ {
metric_key_and_value := strings.Split(metrics[i], ":")
if len(metric_key_and_value) != 2 {
continue
}
value := fmt.Sprintf("%s,%d,%s", metric_key_and_value[0], uint64(time.Now().Unix()), metric_key_and_value[1])
_, err := redisConnection.LPush(channel, value)
if err != nil {
log.Printf("processSerialData() #2: %s", err)
}
_, err = redisConnection.Publish(channel, value)
if err != nil {
log.Printf("processSerialData() #3: %s", err)
}
}
_, err = redisConnection.LTrim(channel, 0, 10)
if err != nil {
log.Printf("processSerialData() #4: %s", err)
}
}
}
示例2: spawnPublisher
func spawnPublisher() error {
var err error
var publisher *redis.Client
publisher = redis.New()
err = publisher.Connect(host, port)
if err != nil {
log.Fatalf("Publisher failed to connect: %s\n", err.Error())
return err
}
log.Println("Publisher connected to redis-server.")
log.Println("Publishing some messages...")
publisher.Publish("channel", "Hello world!")
publisher.Publish("channel", "Do you know how to count?")
for i := 0; i < 3; i++ {
publisher.Publish("channel", i)
}
log.Printf("Closing publisher...\n")
publisher.Quit()
return nil
}