36 lines
515 B
Go
36 lines
515 B
Go
|
package socket
|
||
|
|
||
|
import (
|
||
|
"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(addr string) {
|
||
|
client = redis.NewClient(&redis.Options{
|
||
|
Addr: 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) {
|
||
|
client.Publish("qq", msg)
|
||
|
}
|