diff --git a/clientServer/main.go b/clientServer/main.go new file mode 100644 index 0000000..10669d4 --- /dev/null +++ b/clientServer/main.go @@ -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 + } + + } +} diff --git a/go.mod b/go.mod index 14d8ec8..d455839 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/server/server.go b/server/server.go index 7c82305..739b1bd 100644 --- a/server/server.go +++ b/server/server.go @@ -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 } diff --git a/server/socket/hub.go b/server/socket/hub.go index 916d55a..5b87716 100644 --- a/server/socket/hub.go +++ b/server/socket/hub.go @@ -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) { diff --git a/server/socket/redis.go b/server/socket/redis.go new file mode 100644 index 0000000..b78420b --- /dev/null +++ b/server/socket/redis.go @@ -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) +}