redis: add redis client and broker

websocket
ALI Hamza 2020-01-31 10:24:23 +07:00
parent 3a3871652e
commit ca32ecec3c
No known key found for this signature in database
GPG Key ID: BCA8A46C87798C4C
5 changed files with 69 additions and 1 deletions

@ -0,0 +1,31 @@
package main
import (
"github.com/go-redis/redis/v7"
)
func main() {
client := redis.NewClient(&redis.Options{
Addr: "10.1.3.100:6379",
Password: "",
DB: 0,
})
_, err := client.Ping().Result()
if err != nil {
panic(err)
}
channel := client.Subscribe("qq")
_, err = channel.Receive()
if err != nil {
panic(err)
}
for msg := range channel.Channel() {
if msg.Channel != "qq" {
return
}
}
}

@ -3,7 +3,7 @@ module JISQueueing
go 1.12
require (
github.com/go-redis/redis/v7 v7.0.0-beta.5 // indirect
github.com/go-redis/redis/v7 v7.0.0-beta.5
github.com/gorilla/handlers v1.4.2
github.com/gorilla/mux v1.7.3
github.com/gorilla/websocket v1.4.1

@ -26,6 +26,7 @@ func NewServerMux(debug bool) http.Handler {
router.PathPrefix("/static").Handler(http.FileServer(http.Dir("./static")))
router.PathPrefix("/").HandlerFunc(indexHandler)
socket.StartRedisServer("10.1.3.100:6379")
return router
}

@ -21,6 +21,7 @@ func newTicket(ticket common.Ticket, who *websocket.Conn) {
notifyTicket(conn.Conn)
}
}
sendMessageToRedisChannel(Message{ticket.ID, ticket.Name, ticket.Email})
}
func claimedTicket(ticket common.Ticket, staff common.Staff, table int, who *websocket.Conn) {

@ -0,0 +1,35 @@
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)
}