本文整理汇总了Golang中github.com/pubnub/go/messaging.Pubnub.Publish方法的典型用法代码示例。如果您正苦于以下问题:Golang Pubnub.Publish方法的具体用法?Golang Pubnub.Publish怎么用?Golang Pubnub.Publish使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/pubnub/go/messaging.Pubnub
的用法示例。
在下文中一共展示了Pubnub.Publish方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: PublishSimpleMessage
// PublishSimpleMessage publises a message on a pubnub channel and
// calls the parse method to validate the message subscription.
func PublishSimpleMessage(pubnubInstance *messaging.Pubnub, t *testing.T, channel string, testName string, cipherKey string, responseChannel chan string) {
message := "Test message"
returnChannel := make(chan []byte)
errorChannel := make(chan []byte)
go pubnubInstance.Publish(channel, message, returnChannel, errorChannel)
go ParseSubscribeResponse(pubnubInstance, returnChannel, t, channel, "", testName, cipherKey, responseChannel)
go ParseErrorResponse(errorChannel, responseChannel)
}
示例2: PublishMessages
// PublishMessages calls the publish method of messaging package numberOfMessages times
// and appends the count with the message to distinguish from the others.
//
// Parameters:
// pubnubInstance: a reference of *messaging.Pubnub,
// channel: the pubnub channel to publish the messages,
// t: a reference to *testing.T,
// startMessagesFrom: the message identifer,
// numberOfMessages: number of messages to send,
// message: message to send.
//
// returns a bool if the publish of all messages is successful.
func PublishMessages(pubnubInstance *messaging.Pubnub, channel string, t *testing.T, startMessagesFrom int, numberOfMessages int, message string) bool {
messagesReceived := 0
messageToSend := ""
tOut := messaging.GetNonSubscribeTimeout()
messaging.SetNonSubscribeTimeout(30)
for i := startMessagesFrom; i < startMessagesFrom+numberOfMessages; i++ {
messageToSend = message + strconv.Itoa(i)
returnPublishChannel := make(chan []byte)
errorChannel := make(chan []byte)
go pubnubInstance.Publish(channel, messageToSend, returnPublishChannel, errorChannel)
messagesReceived++
//time.Sleep(500 * time.Millisecond)
time.Sleep(1500 * time.Millisecond)
}
if messagesReceived == numberOfMessages {
return true
}
messaging.SetNonSubscribeTimeout(tOut)
return false
}
示例3: UpdateValuesAndPublish
func (stock *Stock) UpdateValuesAndPublish(pubnub *messaging.Pubnub,
cycle chan bool) {
if stock.CurrentPrice == 0 {
stock.CurrentPrice = stock.InitialPrice
}
rand.Seed(int64(time.Now().Nanosecond()))
change := float64(rand.Intn(stock.Volatility)-stock.Volatility/2) / 100
stock.CurrentPrice = stock.CurrentPrice + float64(change)
delta := stock.CurrentPrice - stock.InitialPrice
percentage := Roundn((1-stock.InitialPrice/stock.CurrentPrice)*100, 2)
vol := Randn(stock.Volatility, 1000) * 10
streamMessage := StreamMessage{
Time: time.Now().Format(TIME_FORMAT),
Price: fmt.Sprintf("%.2f", stock.CurrentPrice),
Delta: fmt.Sprintf("%.2f", delta),
Percentage: fmt.Sprintf("%.2f", percentage),
Vol: vol}
if math.Abs(percentage) > float64(stock.MaxDelta)/100 {
stock.CurrentPrice = stock.InitialPrice
}
errorChannel := make(chan []byte)
successChannel := make(chan []byte)
done := make(chan bool)
go pubnub.Publish(stock.Name, streamMessage, successChannel, errorChannel)
go handleResponse(successChannel, errorChannel,
messaging.GetNonSubscribeTimeout(), done)
sleep := Randn(stock.MinTrade, stock.MaxTrade)
time.Sleep(time.Duration(sleep) * time.Microsecond)
cycle <- <-done
}
示例4: PublishMessages
// PublishMessages calls the publish method of messaging package numberOfMessages times
// and appends the count with the message to distinguish from the others.
//
// Parameters:
// pubnubInstance: a reference of *messaging.Pubnub,
// channel: the pubnub channel to publish the messages,
// t: a reference to *testing.T,
// startMessagesFrom: the message identifer,
// numberOfMessages: number of messages to send,
// message: message to send.
//
// returns a bool if the publish of all messages is successful.
func PublishMessages(pubnubInstance *messaging.Pubnub, channel string,
t *testing.T, startMessagesFrom int, numberOfMessages int,
message string, sleep func(int)) bool {
assert := assert.New(t)
messagesReceived := 0
messageToSend := ""
tOut := messaging.GetNonSubscribeTimeout()
messaging.SetNonSubscribeTimeout(30)
successChannel := make(chan []byte)
errorChannel := make(chan []byte)
for i := startMessagesFrom; i < startMessagesFrom+numberOfMessages; i++ {
messageToSend = message + strconv.Itoa(i)
go pubnubInstance.Publish(channel, messageToSend, successChannel, errorChannel)
select {
case <-successChannel:
messagesReceived++
case err := <-errorChannel:
assert.Fail("Failed to get channel list", string(err))
case <-messaging.Timeout():
assert.Fail("WhereNow timeout")
}
}
sleep(3)
if messagesReceived == numberOfMessages {
return true
}
messaging.SetNonSubscribeTimeout(tOut)
return false
}
示例5: ParseSubscribeMultiplexedResponse
// ParseSubscribeMultiplexedResponse publishes 2 messages on 2 different channels and
// when both the channels are connected
// it reads the responseChannel for the 2 messages. If we get the same 2 messages as response
// the test is passed.
//
// parameters:
// pubnubInstance: an instace of *messaging.Pubnub,
// returnSubscribeChannel: the channel to read the subscribe response on.
// message1: first message to publish.
// message2: second message to publish.
// pubnubChannel1: pubnub Channel 1 to publish the first message.
// pubnubChannel2: pubnub Channel 2 to publish the second message.
// testName: test name.
// responseChannel: the channelto send a response back.
func ParseSubscribeMultiplexedResponse(pubnubInstance *messaging.Pubnub, returnSubscribeChannel chan []byte, message1 string, message2 string, pubnubChannel1 string, pubnubChannel2 string, testName string, responseChannel chan string) {
messageCount := 0
channelCount := 0
for {
value, ok := <-returnSubscribeChannel
if !ok {
break
}
if string(value) != "[]" {
response := fmt.Sprintf("%s", value)
message := "' connected"
messageT1 := "'" + pubnubChannel1 + "' connected"
messageT2 := "'" + pubnubChannel2 + "' connected"
if strings.Contains(response, message) {
if strings.Contains(response, messageT1) {
channelCount++
}
if strings.Contains(response, messageT2) {
channelCount++
}
if channelCount >= 2 {
returnPublishChannel := make(chan []byte)
errorChannelPub := make(chan []byte)
go pubnubInstance.Publish(pubnubChannel1, message1, returnPublishChannel, errorChannelPub)
go ParseResponseDummy(returnPublishChannel)
go ParseResponseDummy(errorChannelPub)
returnPublishChannel2 := make(chan []byte)
errorChannelPub2 := make(chan []byte)
go pubnubInstance.Publish(pubnubChannel2, message2, returnPublishChannel2, errorChannelPub2)
go ParseResponseDummy(returnPublishChannel2)
go ParseResponseDummy(errorChannelPub2)
}
} else {
var s []interface{}
err := json.Unmarshal(value, &s)
if err == nil {
if len(s) > 2 {
if message, ok := s[0].([]interface{}); ok {
if messageT, ok2 := message[0].(string); ok2 {
if (len(message) > 0) && (messageT == message1) && (s[2].(string) == pubnubChannel1) {
messageCount++
}
if (len(message) > 0) && (messageT == message2) && (s[2].(string) == pubnubChannel2) {
messageCount++
}
}
}
}
}
if messageCount >= 2 {
responseChannel <- "Test '" + testName + "': passed."
break
}
}
}
}
}