Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.40 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     pubnub "github.com/pubnub/go"
  6. )
  7.  
  8. func main() {
  9.  
  10.     config := pubnub.NewConfig()
  11.     // config.PublishKey = "my_publish_key"
  12.     config.SubscribeKey = "my_subscribe_key"
  13.  
  14.     pn := pubnub.NewPubNub(config)
  15.  
  16.     listener := pubnub.NewListener()
  17.  
  18.     waitForConnect := make(chan bool)
  19.     waitForMsg := make(chan bool)
  20.  
  21.     go func() {
  22.         for {
  23.             select {
  24.             case status := <-listener.Status:
  25.                 switch status.Category {
  26.                 case pubnub.PNConnectedCategory:
  27.                     // Connect event. You can do stuff like publish, and know you'll get it.
  28.                     // Or just use the connected event to confirm you are subscribed for
  29.                     // UI / internal notifications, etc
  30.                     waitForConnect <- true
  31.                 }
  32.             case msg := <-listener.Message:
  33.                 fmt.Println(" --- MESSAGE: ")
  34.                 fmt.Println(fmt.Sprintf("msg.Channel: %s", msg.Channel))
  35.                 fmt.Println(fmt.Sprintf("msg.Message: %s", msg.Message))
  36.                 fmt.Println(fmt.Sprintf("msg.SubscribedChannel: %s", msg.SubscribedChannel))
  37.                 fmt.Println(fmt.Sprintf("msg.Timetoken: %d", msg.Timetoken))
  38.                 waitForMsg <- true
  39.             case presence := <-listener.Presence:
  40.                 fmt.Println(" --- PRESENCE: ")
  41.                 fmt.Println(fmt.Sprintf("%s", presence))
  42.             }
  43.         }
  44.     }()
  45.  
  46.     pn.AddListener(listener)
  47.  
  48.     pn.Subscribe().
  49.         Channels([]string{"pubnub_onboarding_channel"}).
  50.         Execute()
  51.     <-waitForConnect
  52.     fmt.Println("Connected")
  53.     <-waitForMsg
  54.     fmt.Println("Get Message")
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement