本文整理汇总了Golang中github.com/pubnub/go/messaging.NewPubnub函数的典型用法代码示例。如果您正苦于以下问题:Golang NewPubnub函数的具体用法?Golang NewPubnub怎么用?Golang NewPubnub使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewPubnub函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestServerTime
// TestServerTime calls the GetTime method of the messaging to test the time
func TestServerTime(t *testing.T) {
stop, _ := NewVCRNonSubscribe("fixtures/time", []string{})
defer stop()
pubnubInstance := messaging.NewPubnub(PubKey, SubKey, "", "", false, "testTime")
assert := assert.New(t)
successChannel := make(chan []byte)
errorChannel := make(chan []byte)
go pubnubInstance.GetTime(successChannel, errorChannel)
select {
case value := <-successChannel:
response := string(value)
timestamp, err := strconv.Atoi(strings.Trim(response, "[]\n"))
if err != nil {
assert.Fail(err.Error())
}
assert.NotZero(timestamp)
case err := <-errorChannel:
assert.Fail(string(err))
case <-timeouts(10):
assert.Fail("Getting server timestamp timeout")
}
}
示例2: TestSubscribeGrantPositive
func TestSubscribeGrantPositive(t *testing.T) {
pubnubInstance := messaging.NewPubnub(PamPubKey, PamSubKey, PamSecKey, "", false, "")
channel := "testChannelSubscribeGrantPositive"
ttl := 1
message := fmt.Sprintf(`{"status":200,"service":"Access Manager","message":"Success","payload":{"channels":{"%s":{"r":1,"m":0,"w":1}},"subscribe_key":"%s","ttl":%d,"level":"channel"}}`, channel, PamSubKey, ttl)
message2 := fmt.Sprintf(`{"status":200,"service":"Access Manager","message":"Success","payload":{"channels":{"%s":{"r":0,"m":0,"w":0}},"subscribe_key":"%s","ttl":%d,"level":"channel"}}`, channel, PamSubKey, ttl)
time.Sleep(time.Duration(5) * time.Second)
returnPamChannel := make(chan []byte)
errorChannel := make(chan []byte)
responseChannel := make(chan string)
waitChannel := make(chan string)
go pubnubInstance.GrantSubscribe(channel, true, true, ttl, returnPamChannel, errorChannel)
go ParsePamResponse(returnPamChannel, pubnubInstance, message, channel, "SubscribeGrantPositiveGrant", responseChannel)
go ParseErrorResponse(errorChannel, responseChannel)
go WaitForCompletion(responseChannel, waitChannel)
ParseWaitResponse(waitChannel, t, "SubscribeGrantPositiveGrant")
returnPamChannel2 := make(chan []byte)
errorChannel2 := make(chan []byte)
responseChannel2 := make(chan string)
waitChannel2 := make(chan string)
go pubnubInstance.GrantSubscribe(channel, false, false, -1, returnPamChannel2, errorChannel2)
go ParsePamResponse(returnPamChannel2, pubnubInstance, message2, channel, "SubscribeGrantPositiveRevoke", responseChannel2)
go ParseErrorResponse(errorChannel2, responseChannel2)
go WaitForCompletion(responseChannel2, waitChannel2)
ParseWaitResponse(waitChannel2, t, "SubscribeGrantPositiveRevoke")
}
示例3: ResumeOnReconnect
// ResumeOnReconnect contains the actual impementation of both TestResumeOnReconnectFalse and TestResumeOnReconnectTrue
// the parameter b determines of resume on reconnect setting is true or false.
//
// The test contains a data race
func ResumeOnReconnect(t *testing.T, b bool) {
testName := "ResumeOnReconnectFalse"
if b {
messaging.SetResumeOnReconnect(true)
testName = "ResumeOnReconnectTrue"
} else {
messaging.SetResumeOnReconnect(false)
}
r := GenRandom()
pubnubChannel := fmt.Sprintf("testChannel_subror_%d", r.Intn(20))
pubnubInstance := messaging.NewPubnub(PubKey, SubKey, "", "", false, "")
returnSubscribeChannel := make(chan []byte)
errorChannelSub := make(chan []byte)
responseChannelSub := make(chan string)
waitChannelSub := make(chan string)
messaging.SetSubscribeTimeout(12)
go pubnubInstance.Subscribe(pubnubChannel, "", returnSubscribeChannel, false, errorChannelSub)
go ParseSubscribeForTimetoken(pubnubInstance, pubnubChannel, returnSubscribeChannel, errorChannelSub, testName, responseChannelSub)
go WaitForCompletion(responseChannelSub, waitChannelSub)
ParseWaitResponse(waitChannelSub, t, testName)
messaging.SetSubscribeTimeout(310)
go pubnubInstance.Unsubscribe(pubnubChannel, returnSubscribeChannel, errorChannelSub)
pubnubInstance.CloseExistingConnection()
}
示例4: handler
func handler(w http.ResponseWriter, r *http.Request) {
log.Print("Info: IN handler")
uuid := ""
pubInstance := messaging.NewPubnub(publishKey, subscribeKey, secretKey, "", true, uuid)
if pubInstance == nil {
log.Print("Error: Couldn't create pubnub instance")
http.Error(w, "Couldn't create pubnub instance", http.StatusInternalServerError)
return
}
nuuid := pubInstance.GetUUID()
log.Print("UUID: %s", nuuid)
tok := "s"
err1 := mainTemplate.Execute(w, map[string]string{
"token": tok,
"uuid": nuuid,
"subscribeKey": subscribeKey,
"publishKey": publishKey,
"secretKey": secretKey,
"hostname": fmt.Sprintf("%s%s", hostname, port),
})
if err1 != nil {
log.Print("Error: mainTemplate: %v", err1)
}
}
示例5: SendMultiplexingRequest
// SendMultiplexingRequest is the common method to test TestMultiplexing,
// TestMultiplexingSSL, TestEncryptedMultiplexing, TestEncryptedMultiplexingWithSSL.
//
// It subscribes to 2 channels in the same request and then calls the ParseSubscribeMultiplexedResponse
// for further processing.
//
// Parameters:
// t: *testing.T instance.
// testName: testname for display.
// ssl: ssl setting.
// encrypted: encryption setting.
func SendMultiplexingRequest(t *testing.T, testName string, ssl bool, encrypted bool) {
cipher := ""
if encrypted {
cipher = "enigma"
}
message1 := "message1"
message2 := "message2"
r := GenRandom()
pubnubChannel1 := fmt.Sprintf("testChannel_sub_%d", r.Intn(20))
pubnubChannel2 := fmt.Sprintf("testChannel_sub_%d", r.Intn(20))
pubnubChannel := pubnubChannel1 + "," + pubnubChannel2
pubnubInstance := messaging.NewPubnub(PubKey, SubKey, "", cipher, ssl, "")
returnSubscribeChannel := make(chan []byte)
errorChannelSub := make(chan []byte)
responseChannelSub := make(chan string)
waitChannelSub := make(chan string)
go pubnubInstance.Subscribe(pubnubChannel, "", returnSubscribeChannel, false, errorChannelSub)
go ParseSubscribeMultiplexedResponse(pubnubInstance, returnSubscribeChannel, message1, message2, pubnubChannel1, pubnubChannel2, testName, responseChannelSub)
//go ParseErrorResponse(errorChannelSub, responseChannelSub)
go ParseResponseDummyMessage(errorChannelSub, "aborted", responseChannelSub)
go WaitForCompletion(responseChannelSub, waitChannelSub)
ParseWaitResponse(waitChannelSub, t, testName)
go pubnubInstance.Unsubscribe(pubnubChannel, returnSubscribeChannel, errorChannelSub)
pubnubInstance.CloseExistingConnection()
}
示例6: TestSubscriptionConnectStatus
// TestSubscriptionConnectStatus sends out a subscribe request to a pubnub channel
// and validates the response for the connect status.
func TestSubscriptionConnectStatus(t *testing.T) {
assert := assert.New(t)
stop, _ := NewVCRBoth(
"fixtures/subscribe/connectStatus", []string{"uuid"})
defer stop()
channel := "Channel_ConnectStatus"
uuid := "UUID_ConnectStatus"
pubnubInstance := messaging.NewPubnub(PubKey, SubKey, "", "", false, uuid)
successChannel := make(chan []byte)
errorChannel := make(chan []byte)
unsubscribeSuccessChannel := make(chan []byte)
unsubscribeErrorChannel := make(chan []byte)
go pubnubInstance.Subscribe(channel, "", successChannel, false, errorChannel)
select {
case resp := <-successChannel:
response := fmt.Sprintf("%s", resp)
if response != "[]" {
message := "'" + channel + "' connected"
assert.Contains(response, message)
}
case err := <-errorChannel:
if !IsConnectionRefusedError(err) {
assert.Fail(string(err))
}
case <-timeouts(3):
assert.Fail("Subscribe timeout 3s")
}
go pubnubInstance.Unsubscribe(channel, unsubscribeSuccessChannel, unsubscribeErrorChannel)
ExpectUnsubscribedEvent(t, channel, "", unsubscribeSuccessChannel, unsubscribeErrorChannel)
}
示例7: DetailedHistoryFor10Messages
// DetailedHistoryFor10Messages is a common method used by both TestDetailedHistoryFor10EncryptedMessages
// and TestDetailedHistoryFor10Messages to publish's 10 messages to a pubnub channel, and after that
// call the history method of the messaging package to fetch last 10 messages. These received
// messages are compared to the messages sent and if all match test is successful.
func DetailedHistoryFor10Messages(t *testing.T, cipherKey string, testName string) {
numberOfMessages := 10
startMessagesFrom := 0
pubnubInstance := messaging.NewPubnub(PubKey, SubKey, SecKey, cipherKey, false, "")
message := "Test Message "
r := GenRandom()
channel := fmt.Sprintf("testChannel_dh_%d", r.Intn(20))
messagesSent := PublishMessages(pubnubInstance, channel, t, startMessagesFrom, numberOfMessages, message)
if messagesSent {
returnHistoryChannel := make(chan []byte)
errorChannel := make(chan []byte)
responseChannel := make(chan string)
waitChannel := make(chan string)
go pubnubInstance.History(channel, numberOfMessages, 0, 0, false, returnHistoryChannel, errorChannel)
go ParseHistoryResponseForMultipleMessages(returnHistoryChannel, channel, message, testName, startMessagesFrom, numberOfMessages, cipherKey, responseChannel)
go ParseErrorResponse(errorChannel, responseChannel)
go WaitForCompletion(responseChannel, waitChannel)
ParseWaitResponse(waitChannel, t, testName)
} else {
t.Error("Test '" + testName + "': failed.")
}
}
示例8: TestSuccessCodeAndInfoForComplexMessage
// TestSuccessCodeAndInfoForComplexMessage sends out a complex message to the pubnub channel
func TestSuccessCodeAndInfoForComplexMessage(t *testing.T) {
assert := assert.New(t)
stop, _ := NewVCRNonSubscribe(
"fixtures/publish/successCodeAndInfoForComplexMessage", []string{"uuid"})
defer stop()
pubnubInstance := messaging.NewPubnub(PubKey, SubKey, "", "", false, "")
channel := "successCodeAndInfoForComplexMessage"
customStruct := CustomStruct{
Foo: "hi!",
Bar: []int{1, 2, 3, 4, 5},
}
successChannel := make(chan []byte)
errorChannel := make(chan []byte)
go pubnubInstance.Publish(channel, customStruct, successChannel, errorChannel)
select {
case msg := <-successChannel:
assert.Contains(string(msg), "1,")
assert.Contains(string(msg), "\"Sent\",")
case err := <-errorChannel:
assert.Fail(string(err))
case <-timeout():
assert.Fail("Publish timeout")
}
}
示例9: TestSuccessCodeAndInfoWithEncryption
// TestSuccessCodeAndInfoWithEncryption sends out an encrypted
// message to the pubnub channel
func TestSuccessCodeAndInfoWithEncryption(t *testing.T) {
assert := assert.New(t)
stop, _ := NewVCRNonSubscribe(
"fixtures/publish/successCodeAndInfoWithEncryption", []string{"uuid"})
defer stop()
pubnubInstance := messaging.NewPubnub(PubKey, SubKey, "", "enigma", false, "")
channel := "successCodeAndInfo"
message := "Pubnub API Usage Example"
successChannel := make(chan []byte)
errorChannel := make(chan []byte)
go pubnubInstance.Publish(channel, message, successChannel, errorChannel)
select {
case msg := <-successChannel:
assert.Contains(string(msg), "1,")
assert.Contains(string(msg), "\"Sent\",")
case err := <-errorChannel:
assert.Fail(string(err))
case <-timeout():
assert.Fail("Publish timeout")
}
}
示例10: TestWildcardSubscriptionConnectedAndUnsubscribedSingle
func TestWildcardSubscriptionConnectedAndUnsubscribedSingle(t *testing.T) {
assert := assert.New(t)
stop, _ := NewVCRBoth("fixtures/wildcard/connAndUns", []string{"uuid"})
defer stop()
major := "Channel_ConnAndUns"
wildcard := fmt.Sprintf("%s.*", major)
uuid := "UUID_ConnAndUns"
pubnubInstance := messaging.NewPubnub(PubKey, SubKey, "", "", false, uuid)
subscribeSuccessChannel := make(chan []byte)
subscribeErrorChannel := make(chan []byte)
successChannel := make(chan []byte)
errorChannel := make(chan []byte)
go pubnubInstance.Subscribe(wildcard, "",
subscribeSuccessChannel, false, subscribeErrorChannel)
select {
case msg := <-subscribeSuccessChannel:
val := string(msg)
assert.Equal(fmt.Sprintf(
"[1, \"Subscription to channel '%s' connected\", \"%s\"]",
wildcard, wildcard), val)
case err := <-subscribeErrorChannel:
assert.Fail(string(err))
}
go pubnubInstance.Unsubscribe(wildcard, successChannel, errorChannel)
ExpectUnsubscribedEvent(t, wildcard, "", successChannel, errorChannel)
// pubnubInstance.CloseExistingConnection()
}
示例11: TestGroupSubscriptionToNotExistingChannelGroup
func TestGroupSubscriptionToNotExistingChannelGroup(t *testing.T) {
assert := assert.New(t)
stop, sleep := NewVCRBoth(
"fixtures/groups/notExistingCG", []string{"uuid"})
defer stop()
group := "Group_NotExistingCG"
uuid := "UUID_NotExistingCG"
pubnub := messaging.NewPubnub(PubKey, SubKey, "", "", false, uuid)
successChannel := make(chan []byte)
errorChannel := make(chan []byte)
unsubscribeSuccessChannel := make(chan []byte)
unsubscribeErrorChannel := make(chan []byte)
removeChannelGroups(pubnub, []string{group})
sleep(2)
go pubnub.ChannelGroupSubscribe(group, successChannel, errorChannel)
select {
case response := <-successChannel:
assert.Fail("Received success message while expecting error", string(response))
case err := <-errorChannel:
assert.Contains(string(err), "Channel group or groups result in empty subscription set")
assert.Contains(string(err), group)
}
go pubnub.ChannelGroupUnsubscribe(group, unsubscribeSuccessChannel, unsubscribeErrorChannel)
ExpectUnsubscribedEvent(t, "", group, unsubscribeSuccessChannel,
unsubscribeErrorChannel)
}
示例12: TestGroupSubscriptionNotSubscribed
func TestGroupSubscriptionNotSubscribed(t *testing.T) {
assert := assert.New(t)
stop, sleep := NewVCRNonSubscribe(
"fixtures/groups/notSubscribed", []string{"uuid"})
defer stop()
group := "Group_NotSubscribed"
uuid := "UUID_NotSubscribed"
pubnub := messaging.NewPubnub(PubKey, SubKey, "", "", false, uuid)
createChannelGroups(pubnub, []string{group})
defer removeChannelGroups(pubnub, []string{group})
sleep(2)
successChannel := make(chan []byte)
errorChannel := make(chan []byte)
go pubnub.ChannelGroupUnsubscribe(group, successChannel, errorChannel)
select {
case response := <-successChannel:
assert.Fail("Received success message while expecting error", string(response))
case err := <-errorChannel:
assert.Contains(string(err), "Subscription to channel group")
assert.Contains(string(err), "not subscribed")
}
pubnub.CloseExistingConnection()
}
示例13: SetUpChannelGroup
func SetUpChannelGroup() {
errorChannel := make(chan []byte)
successChannel := make(chan []byte)
done := make(chan bool)
pubnub := messaging.NewPubnub(config.Keys.Pub, config.Keys.Sub, "", "",
false, "")
pubnub.SetAuthenticationKey(bootstrapAuth)
// Remove Group
go pubnub.ChannelGroupRemoveGroup(config.StocksChannelGroup,
successChannel, errorChannel)
go handleResponse(successChannel, errorChannel,
messaging.GetNonSubscribeTimeout(), done)
<-done
// Create it from the scratch
go pubnub.ChannelGroupAddChannel(config.StocksChannelGroup, stockNames,
successChannel, errorChannel)
go handleResponse(successChannel, errorChannel,
messaging.GetNonSubscribeTimeout(), done)
<-done
}
示例14: grantSubscribe
func grantSubscribe(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
ch := q.Get("ch")
read := q.Get("r")
write := q.Get("w")
ttl := q.Get("ttl")
bRead := false
if read == "1" {
bRead = true
}
bWrite := false
if write == "1" {
bWrite = true
}
iTTL := 1440
if ival, err := strconv.Atoi(ttl); err == nil {
iTTL = ival
}
uuid := q.Get("uuid")
pubInstance := messaging.NewPubnub(publishKey, subscribeKey, secretKey, "", true, uuid)
errorChannel := make(chan []byte)
successChannel := make(chan []byte)
go pubInstance.GrantSubscribe(ch, bRead, bWrite, iTTL, "", successChannel, errorChannel)
handleResult(w, r, uuid, successChannel, errorChannel, messaging.GetNonSubscribeTimeout(), "Revoke Subscribe")
}
示例15: TestUnsubscribeChannel
// TestUnsubscribe will subscribe to a pubnub channel and then send an unsubscribe request
// The response should contain 'unsubscribed'
func TestUnsubscribeChannel(t *testing.T) {
assert := assert.New(t)
stop, sleep := NewVCRBoth(
"fixtures/unsubscribe/channel", []string{"uuid"})
defer stop()
channel := "Channel_UnsubscribeChannel"
uuid := "UUID_UnsubscribeChannel"
pubnubInstance := messaging.NewPubnub(PubKey, SubKey, "", "", false, uuid)
subscribeSuccessChannel := make(chan []byte)
subscribeErrorChannel := make(chan []byte)
unSubscribeSuccessChannel := make(chan []byte)
unSubscribeErrorChannel := make(chan []byte)
go pubnubInstance.Subscribe(channel, "", subscribeSuccessChannel,
false, subscribeErrorChannel)
select {
case msg := <-subscribeSuccessChannel:
val := string(msg)
assert.Equal(val, fmt.Sprintf(
"[1, \"Subscription to channel '%s' connected\", \"%s\"]",
channel, channel))
case err := <-subscribeErrorChannel:
assert.Fail(string(err))
}
sleep(2)
go pubnubInstance.Unsubscribe(channel, unSubscribeSuccessChannel,
unSubscribeErrorChannel)
select {
case msg := <-unSubscribeSuccessChannel:
val := string(msg)
assert.Equal(val, fmt.Sprintf(
"[1, \"Subscription to channel '%s' unsubscribed\", \"%s\"]",
channel, channel))
case err := <-unSubscribeErrorChannel:
assert.Fail(string(err))
}
select {
case ev := <-unSubscribeSuccessChannel:
var event messaging.PresenceResonse
err := json.Unmarshal(ev, &event)
if err != nil {
assert.Fail(err.Error())
}
assert.Equal("leave", event.Action)
assert.Equal(200, event.Status)
case err := <-unSubscribeErrorChannel:
assert.Fail(string(err))
}
// pubnubInstance.CloseExistingConnection()
}