40 lines
705 B
Go
40 lines
705 B
Go
package socket
|
|
|
|
import (
|
|
json "encoding/json"
|
|
"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() {
|
|
client = redis.NewClient(&redis.Options{
|
|
Addr: config.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(config.Redis.Channel, string(mesg))
|
|
if status.Err() != nil {
|
|
println("Error while sending message to redis:", status.Err().Error())
|
|
}
|
|
} |