This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
JISQueueing/server/socket/redis.go

41 lines
755 B
Go

package socket
import (
json "encoding/json"
"github.com/go-redis/redis/v7"
"github.com/spf13/viper"
)
var client *redis.Client
var channel *redis.PubSub
type Message struct {
ID int
Name string
Email string
}
func StartRedisServer() {
client = redis.NewClient(&redis.Options{
Addr: viper.GetString("redis.addr"),
})
_, err := client.Ping().Result()
if err != nil {
panic(err)
}
channel = client.Subscribe("qq")
_, err = channel.Receive()
if err != nil {
panic(err)
}
}
func sendMessageToRedisChannel(msg Message) {
mesg, _ := json.Marshal(msg)
status := client.Publish(viper.GetString("viper.channel"), string(mesg))
if status.Err() != nil {
println("Error while sending message to redis:", status.Err().Error())
}
}