本文整理匯總了Golang中github.com/pubnub/go/pubnubMessaging.Pubnub.Subscribe方法的典型用法代碼示例。如果您正苦於以下問題:Golang Pubnub.Subscribe方法的具體用法?Golang Pubnub.Subscribe怎麽用?Golang Pubnub.Subscribe使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/pubnub/go/pubnubMessaging.Pubnub
的用法示例。
在下文中一共展示了Pubnub.Subscribe方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ParseSubscribeResponseForPresence
// ParseSubscribeResponseForPresence will look for the connection status in the response
// received on the go channel.
func ParseSubscribeResponseForPresence(pubnubInstance *pubnubMessaging.Pubnub, customUuid string, returnChannel chan []byte, channel string, testName string, responseChannel chan string) {
for {
value, ok := <-returnChannel
if !ok {
break
}
//response := fmt.Sprintf("%s", value)
if string(value) != "[]" {
if (testName == "CustomUuid") || (testName == "HereNow") || (testName == "HereNowWithCipher") {
response := fmt.Sprintf("%s", value)
message := "'" + channel + "' connected"
messageReconn := "'" + channel + "' reconnected"
if (strings.Contains(response, message)) || (strings.Contains(response, messageReconn)) {
errorChannel := make(chan []byte)
returnChannel := make(chan []byte)
time.Sleep(3 * time.Second)
go pubnubInstance.HereNow(channel, returnChannel, errorChannel)
go ParseHereNowResponse(returnChannel, channel, customUuid, testName, responseChannel)
go ParseErrorResponse(errorChannel, responseChannel)
break
}
} else {
response := fmt.Sprintf("%s", value)
message := "'" + channel + "' connected"
messageReconn := "'" + channel + "' reconnected"
//fmt.Println("Test3 '" + testName + "':" +response)
if (strings.Contains(response, message)) || (strings.Contains(response, messageReconn)) {
errorChannel2 := make(chan []byte)
returnSubscribeChannel := make(chan []byte)
time.Sleep(1 * time.Second)
go pubnubInstance.Subscribe(channel, "", returnSubscribeChannel, false, errorChannel2)
go ParseResponseDummy(returnSubscribeChannel)
go ParseResponseDummy(errorChannel2)
} else {
if testName == "Presence" {
data, _, returnedChannel, err2 := pubnubMessaging.ParseJson(value, "")
var occupants []struct {
Action string
Uuid string
Timestamp float64
Occupancy int
}
if err2 != nil {
responseChannel <- "Test '" + testName + "': failed. Message: 1 :" + err2.Error()
break
}
//fmt.Println("Test3 '" + testName + "':" +data)
err := json.Unmarshal([]byte(data), &occupants)
if err != nil {
//fmt.Println("err '" + testName + "':",err)
responseChannel <- "Test '" + testName + "': failed. Message: 2 :" + err.Error()
break
} else {
channelSubRepsonseReceived := false
for i := 0; i < len(occupants); i++ {
if (occupants[i].Action == "join") && occupants[i].Uuid == customUuid {
channelSubRepsonseReceived = true
break
}
}
if !channelSubRepsonseReceived {
responseChannel <- "Test '" + testName + "': failed. Message: err3"
break
}
if channel == returnedChannel {
responseChannel <- "Test '" + testName + "': passed."
break
} else {
responseChannel <- "Test '" + testName + "': failed. Message: err4"
break
}
}
}
}
}
}
}
}
示例2: ParseSubscribeResponse
// ParseSubscribeResponse reads the response from the go channel and unmarshal's it.
// It is used by multiple test cases and acts according to the testcase names.
// The idea is to parse each message in the response based on the type of message
// and test against the sent message. If both match the test case is successful.
// _publishSuccessMessage is defined in the common.go file.
func ParseSubscribeResponse(pubnubInstance *pubnubMessaging.Pubnub, returnChannel chan []byte, t *testing.T, channel string, message string, testName string, cipherKey string, responseChannel chan string) {
for {
value, ok := <-returnChannel
if !ok {
break
}
if string(value) != "[]" {
response := fmt.Sprintf("%s", value)
//fmt.Println("Response1:", response)
if (testName == "SubscriptionConnectedForComplex") || (testName == "SubscriptionConnectedForComplexWithCipher") {
message = "'" + channel + "' connected"
if strings.Contains(response, message) {
PublishComplexMessage(pubnubInstance, t, channel, _publishSuccessMessage, cipherKey, responseChannel)
} else {
//fmt.Println("resp:", response)
if ParseSubscribeData(t, value, testName, cipherKey, responseChannel) {
responseChannel <- "Test '" + testName + "': passed."
} else {
responseChannel <- "Test '" + testName + "': failed."
}
break
}
} else if (testName == "SubscriptionConnectedForSimple") || (testName == "SubscriptionConnectedForSimpleWithCipher") {
message = "'" + channel + "' connected"
if strings.Contains(response, message) {
PublishSimpleMessage(pubnubInstance, t, channel, _publishSuccessMessage, cipherKey, responseChannel)
} else {
if ParseSubscribeData(t, value, testName, cipherKey, responseChannel) {
responseChannel <- "Test '" + testName + "': passed."
} else {
responseChannel <- "Test '" + testName + "': failed."
}
break
}
} else if testName == "SubscriptionAlreadySubscribed" {
message = "'" + channel + "' connected"
if strings.Contains(response, message) {
returnSubscribeChannel2 := make(chan []byte)
errorChannel2 := make(chan []byte)
go pubnubInstance.Subscribe(channel, "", returnSubscribeChannel2, false, errorChannel2)
go ParseSubscribeResponse(pubnubInstance, errorChannel2, t, channel, "already subscribed", "SubscriptionAlreadySubscribedResponse", "", responseChannel)
go ParseResponseDummy(returnSubscribeChannel2)
}
break
} else if testName == "SubscriptionAlreadySubscribedResponse" {
message = "'" + channel + "' already subscribed"
if strings.Contains(response, message) {
responseChannel <- "Test '" + testName + "': passed."
} else {
responseChannel <- "Test '" + testName + "': failed."
//t.Error("Test '" + testName + "': failed.");
}
break
} else if testName == "SubscriptionConnectStatus" {
message = "'" + channel + "' connected"
if strings.Contains(response, message) {
responseChannel <- "Test '" + testName + "': passed."
} else {
responseChannel <- "Test '" + testName + "': failed."
//t.Error("Test '" + testName + "': failed.");
}
break
}
}
}
}