Esempio n. 1
0
func InitRedis() {
	redisnum := utils.Cfg.GetInt("redisNum")
	for i := 0; i < redisnum; i++ {
		ip := utils.Cfg.GetString("redisIp" + strconv.Itoa(i))
		port := utils.Cfg.GetInt("redisPort" + strconv.Itoa(i))
		spec[i] = redis.DefaultSpec()
		spec[i].Host(ip)
		spec[i].Port(port)
		sub[i], errtopic = redis.NewPubSubClientWithSpec(spec[i])
		if errtopic != nil {
			//			log.Println("failed to create the sync client", e)
			fmt.Printf("Error ", errtopic)
		}
		sub[i].Subscribe(utils.Cfg.GetString("slavename")) //队列名为从的名字
		go getmessage(i)
	}
}
Esempio n. 2
0
func connectClient(chanKey string) redis.PubSubChannel {

	spec := redis.DefaultSpec().Password("go-redis")
	client, e := redis.NewPubSubClientWithSpec(spec)
	if e != nil {
		fmt.Print("Error creating client for: ", e)
	}
	defer client.Quit()
	fmt.Println("before subbing")
	client.Subscribe("chat")
	fmt.Println("after subbing")
	return client.Messages("chat")
	//fmt.Println(string(<-client.Messages("chat")))
	//fmt.Println(string(<-client.Messages("chat")))
	//for {
	//	chatbuf := bytes.NewBuffer(<-client.Messages("chat"))
	//	chatbuf.WriteTo(os.Stdout)
	//	}
}