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

40 lines
705 B
Go

2020-01-31 03:24:23 +07:00
package socket
import (
json "encoding/json"
2020-01-31 03:24:23 +07:00
"github.com/go-redis/redis/v7"
)
var client *redis.Client
var channel *redis.PubSub
type Message struct {
ID int
Name string
Email string
}
func StartRedisServer() {
2020-01-31 03:24:23 +07:00
client = redis.NewClient(&redis.Options{
2020-02-05 04:55:03 +07:00
Addr: config.Redis.Addr,
2020-01-31 03:24:23 +07:00
})
_, 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)
2020-02-05 04:55:03 +07:00
status := client.Publish(config.Redis.Channel, string(mesg))
if status.Err() != nil {
println("Error while sending message to redis:", status.Err().Error())
}
}