本文整理匯總了Golang中github.com/garyburd/redigo/redis.PubSubConn類的典型用法代碼示例。如果您正苦於以下問題:Golang PubSubConn類的具體用法?Golang PubSubConn怎麽用?Golang PubSubConn使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了PubSubConn類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestPublishEnd
func TestPublishEnd(t *testing.T) {
time.Sleep(1 * time.Second)
emitter, _ := NewEmitter(&EmitterOpts{
Host: "localhost",
Port: 6379,
})
defer emitter.Close()
c, _ := redis.Dial("tcp", "localhost:6379")
defer c.Close()
psc := redis.PubSubConn{Conn: c}
psc.Subscribe("socket.io#emitter")
emitter.Emit("finish")
for {
switch v := psc.Receive().(type) {
case redis.Message:
isContain := strings.Contains(string(v.Data), "finish")
if !isContain {
t.Errorf("%s not contains end", v.Data)
return
} else {
return
}
}
}
}
示例2: ReceiveMessages
// ReceiveMessages : Receive messages fron master_messages redis channel
func ReceiveMessages(newSlaveChannel chan string, ipAddress string) {
conn, err := redisurl.ConnectToURL(redisURL)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Close only when function exits
defer conn.Close()
// Creating a pubsubConn for master messages
pubsubConn := redis.PubSubConn{Conn: conn}
pubsubConn.Subscribe(masterMessageQueue)
for {
switch val := pubsubConn.Receive().(type) {
case redis.Message:
// If the data being received is a text message then push it to the channel
newSlaveChannel <- string(val.Data)
case redis.Subscription:
//Handle Subscription here
case error:
return
}
}
}
示例3: MonitorSentinel
// Monitor sentinel
func MonitorSentinel() {
redisConn := gRedisPool.Get()
defer redisConn.Close()
psc := redis.PubSubConn{redisConn}
psc.PSubscribe("*")
runflag := true
for runflag {
switch v := psc.Receive().(type) {
case redis.Message:
log.Infof("Type Message>>channel %s, message: %s", v.Channel, v.Data)
case redis.Subscription:
log.Infof("Type Subscribe>>channel %s, kind %s, count %d", v.Channel, v.Kind, v.Count)
gRecoveryChan <- RECOVERY_TYPE_REDIS
case error:
log.Error("MonitorSentinel ERROR")
runflag = false
// Should re psubscrebe
case redis.PMessage:
log.Infof("Type PMessage>>channel %s, pattern %s, data %s", v.Channel, v.Pattern, v.Data)
ParsePMessage(v)
default:
log.Warnf("Unkown Message Type of psubscribe")
}
}
}
示例4: Test_Redis
func Test_Redis(t *testing.T) {
pool := newPool("123.56.98.103:6379", "")
conn := pool.Get()
fmt.Println(conn)
// go func() {
cc, err := redis.Dial("tcp", "123.56.98.103:6379")
if err != nil {
panic(err)
}
cc.Do("select", "9")
psc := redis.PubSubConn{Conn: cc}
psc.Subscribe("products")
time.Sleep(1 * time.Second)
for {
switch v := psc.Receive().(type) {
case redis.Subscription:
fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count)
case redis.Message: //單個訂閱subscribe
fmt.Printf("%s: message: %s\n", v.Channel, v.Data)
case redis.PMessage: //模式訂閱psubscribe
fmt.Printf("PMessage: %s %s %s\n", v.Pattern, v.Channel, v.Data)
case error:
fmt.Println("error", v)
time.Sleep(1 * time.Second)
}
}
// }()
}
示例5: run
// run receives pubsub messages from Redis after establishing a connection.
// When a valid message is received it is broadcast to all connected websockets
func (rr *redisReceiver) run() {
conn := rr.pool.Get()
defer conn.Close()
psc := redis.PubSubConn{conn}
psc.Subscribe(CHANNEL)
for {
switch v := psc.Receive().(type) {
case redis.Message:
log.WithFields(log.Fields{
"channel": v.Channel,
"message": string(v.Data),
}).Println("Redis Message Received")
msg, err := validateMessage(v.Data)
if err != nil {
log.WithFields(log.Fields{
"err": err,
"data": v.Data,
"msg": msg,
}).Error("Error unmarshalling message from Redis")
continue
}
rr.broadcast(v.Data)
case redis.Subscription:
log.WithFields(log.Fields{
"channel": v.Channel,
"kind": v.Kind,
"count": v.Count,
}).Println("Redis Subscription Received")
case error:
log.WithField("err", v).Errorf("Error while subscribed to Redis channel %s", CHANNEL)
default:
log.WithField("v", v).Println("Unknown Redis receive during subscription")
}
}
}
示例6: Run
func (rpsi *RedisPubSubInput) Run(ir pipeline.InputRunner, h pipeline.PluginHelper) error {
var (
dRunner pipeline.DecoderRunner
decoder pipeline.Decoder
pack *pipeline.PipelinePack
e error
ok bool
)
// Get the InputRunner's chan to receive empty PipelinePacks
packSupply := ir.InChan()
if rpsi.conf.DecoderName != "" {
if dRunner, ok = h.DecoderRunner(rpsi.conf.DecoderName, fmt.Sprintf("%s-%s", ir.Name(), rpsi.conf.DecoderName)); !ok {
return fmt.Errorf("Decoder not found: %s", rpsi.conf.DecoderName)
}
decoder = dRunner.Decoder()
}
//Connect to the channel
psc := redis.PubSubConn{Conn: rpsi.conn}
psc.PSubscribe(rpsi.conf.Channel)
for {
switch n := psc.Receive().(type) {
case redis.PMessage:
// Grab an empty PipelinePack from the InputRunner
pack = <-packSupply
pack.Message.SetType("redis_pub_sub")
pack.Message.SetLogger(n.Channel)
pack.Message.SetPayload(string(n.Data))
pack.Message.SetTimestamp(time.Now().UnixNano())
var packs []*pipeline.PipelinePack
if decoder == nil {
packs = []*pipeline.PipelinePack{pack}
} else {
packs, e = decoder.Decode(pack)
}
if packs != nil {
for _, p := range packs {
ir.Inject(p)
}
} else {
if e != nil {
ir.LogError(fmt.Errorf("Couldn't parse Redis message: %s", n.Data))
}
pack.Recycle(nil)
}
case redis.Subscription:
ir.LogMessage(fmt.Sprintf("Subscription: %s %s %d\n", n.Kind, n.Channel, n.Count))
if n.Count == 0 {
return errors.New("No channel to subscribe")
}
case error:
fmt.Printf("error: %v\n", n)
return n
}
}
return nil
}
示例7: SubscribeToSentinel
func SubscribeToSentinel() {
sentinel := GetSentinel()
c, err := redis.Dial("tcp", sentinel)
if err != nil {
Fatal("Cannot connect to redis sentinel:", sentinel)
}
err = ValidateCurrentMaster()
if err != nil {
Fatal("Cannot switch to current master")
}
psc := redis.PubSubConn{c}
Debug("Subscribing to sentinel (+switch-master).")
psc.Subscribe("+switch-master")
for {
switch v := psc.Receive().(type) {
case redis.Message:
Debug(fmt.Sprintf("%s: message: %s", v.Channel, v.Data))
data := strings.Split(string(v.Data), string(' '))
SwitchMaster(data[0], data[3], data[4])
case redis.Subscription:
Debug(fmt.Sprintf("%s: %s %d", v.Channel, v.Kind, v.Count))
case error:
Fatal("Error with redis connection:", psc)
}
}
}
示例8: ProcessNewBlock
func ProcessNewBlock(conf *Config, rpool *redis.Pool, spool *redis.Pool) {
log.Println("ProcessNewBlock startup")
conn := rpool.Get()
defer conn.Close()
psc := redis.PubSubConn{Conn: conn}
psc.Subscribe("btcplex:blocknotify")
for {
switch v := psc.Receive().(type) {
case redis.Message:
hash := string(v.Data)
log.Printf("Processing new block: %v\n", hash)
c := rpool.Get()
newblock, err := SaveBlockFromRPC(conf, spool, hash)
if err != nil {
log.Printf("Error processing new block: %v\n", err)
} else {
// Once the block is processed, we can publish it as btcplex own blocknotify
c.Do("PUBLISH", "btcplex:blocknotify2", hash)
newblockjson, _ := json.Marshal(newblock)
c.Do("PUBLISH", "btcplex:newblock", string(newblockjson))
}
c.Close()
}
}
}
示例9: NewReceiverFunc
// NewReceiverFunc returns the function that
// listens of redis for start/stop commands
func NewReceiverFunc(redisAddr string, redisDB int) pingd.Receiver {
return func(startHostCh, stopHostCh chan<- pingd.Host) {
conPubSub, err := redis.Dial("tcp", redisAddr)
if err != nil {
log.Panicln(err)
}
connKV, err := redis.Dial("tcp", redisAddr)
if err != nil {
log.Panicln(err)
}
servername, _ := os.Hostname()
conPubSub.Do("CLIENT", "SETNAME", "receive-"+servername)
conPubSub.Do("SELECT", redisDB)
connKV.Do("CLIENT", "SETNAME", "receive-"+servername)
connKV.Do("SELECT", redisDB)
psc := redis.PubSubConn{conPubSub}
psc.Subscribe(startRK, stopRK)
for {
switch n := psc.Receive().(type) {
case redis.Message:
if n.Channel == startRK {
host := string(n.Data)
down := false
if strings.HasSuffix(host, downSuffix) {
down = true
host = strings.Replace(host, downSuffix, "", 1)
}
// Add to the list of pinged hosts
_, err := connKV.Do("SADD", hostListRK, host)
if err != nil {
log.Panicln(err)
}
startHostCh <- pingd.Host{Host: host, Down: down}
} else if n.Channel == stopRK {
host := string(n.Data)
// Remove from the list of pinged hosts
_, err := connKV.Do("SREM", hostListRK, host)
if err != nil {
log.Panicln(err)
}
stopHostCh <- pingd.Host{Host: host}
}
case redis.PMessage:
case redis.Subscription:
log.Println("BOOT Listening to " + n.Channel)
case error:
log.Printf("error: %v\n", n)
return
}
}
}
}
示例10: redisSubscriber
func redisSubscriber() {
conn, err := redis.Dial("tcp", "redis:6379")
if err != nil {
panic("I don't want to live on this planet anymore")
}
psc := redis.PubSubConn{conn}
psc.Subscribe("candy")
for {
switch v := psc.Receive().(type) {
case redis.Message:
fmt.Printf("%s: message: %s\n", v.Channel, v.Data)
if v.Channel == "candy" {
var c Candy
err := json.Unmarshal(v.Data, &c)
if err != nil {
log.Printf("Seems our redis is sick! In the evening we'll get some schnaps to ease the pain!")
continue
}
redisChan <- c
}
case redis.Subscription:
fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count)
case error:
log.Println(v.Error())
}
}
}
示例11: main3
func main3() {
//INIT OMIT
c, err := redis.Dial("tcp", ":6379")
if err != nil {
panic(err)
}
defer c.Close()
//set
c.Do("SET", "message1", "Hello World")
//get
world, err := redis.String(c.Do("GET", "message1"))
if err != nil {
fmt.Println("key not found")
}
fmt.Println(world)
//ENDINIT OMIT
psc := redis.PubSubConn{c}
psc.PSubscribe("bigbluebutton:to-bbb-apps:system")
for {
switch v := psc.Receive().(type) {
case redis.Message:
fmt.Printf("%s: message: %s\n", v.Channel, v.Data)
case redis.PMessage:
fmt.Printf("PMessage: %s %s %s\n", v.Pattern, v.Channel, v.Data)
case redis.Subscription:
fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count)
case error:
fmt.Printf("error: %v\n", v)
}
}
}
示例12: readFromRedis
func (c *connection) readFromRedis() {
conn := pool.Get()
defer conn.Close()
psc := redis.PubSubConn{conn}
if err := psc.Subscribe(c.subscription); err != nil {
log.Fatalf("Failed to subscribe to %v: %v", c.subscription, err)
return
}
log.Printf("Connected to redis channel %v", c.subscription)
for {
switch v := psc.Receive().(type) {
case redis.Message:
log.Printf("Got a redis message: %v", v)
c.send <- v.Data
case redis.Subscription:
log.Print("Got a redis subscription")
// XXX nop?
case error:
log.Fatalf("Error reading messages: %v", v)
default:
log.Fatalf("Got an unknown redis message type: %v", v)
}
}
}
示例13: ReadFrames
func (self *Server) ReadFrames() {
c, err := redis.Dial("tcp", ":6379")
if err != nil {
panic(err)
}
psc := redis.PubSubConn{c}
psc.Subscribe("pokemon.streams.frames")
for {
switch v := psc.Receive().(type) {
case redis.Message:
frame := &Frame{}
err := json.Unmarshal(v.Data, &frame)
if err != nil {
continue
}
self.sendAll <- frame.Delta
self.frame = "0\t" + frame.Dithered
case redis.Subscription:
fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count)
case error:
panic(v)
}
}
}
示例14: RunOnce
func (cs *CustomerService) RunOnce() bool {
c, err := redis.Dial("tcp", config.redis_address)
if err != nil {
log.Info("dial redis error:", err)
return false
}
psc := redis.PubSubConn{c}
psc.Subscribe("application_update")
cs.Clear()
for {
switch v := psc.Receive().(type) {
case redis.Message:
if v.Channel == "application_update" {
cs.HandleUpdate(string(v.Data))
} else {
log.Infof("%s: message: %s\n", v.Channel, v.Data)
}
case redis.Subscription:
log.Infof("%s: %s %d\n", v.Channel, v.Kind, v.Count)
case error:
log.Info("error:", v)
return true
}
}
}
示例15: RunOnce
func (group_manager *GroupManager) RunOnce() bool {
c, err := redis.Dial("tcp", config.redis_address)
if err != nil {
log.Info("dial redis error:", err)
return false
}
psc := redis.PubSubConn{c}
psc.Subscribe("group_create", "group_disband", "group_member_add", "group_member_remove")
group_manager.Reload()
for {
switch v := psc.Receive().(type) {
case redis.Message:
if v.Channel == "group_create" {
group_manager.HandleCreate(string(v.Data))
} else if v.Channel == "group_disband" {
group_manager.HandleDisband(string(v.Data))
} else if v.Channel == "group_member_add" {
group_manager.HandleMemberAdd(string(v.Data))
} else if v.Channel == "group_member_remove" {
group_manager.HandleMemberRemove(string(v.Data))
} else {
log.Infof("%s: message: %s\n", v.Channel, v.Data)
}
case redis.Subscription:
log.Infof("%s: %s %d\n", v.Channel, v.Kind, v.Count)
case error:
log.Info("error:", v)
return true
}
}
}