示例#1
0
def main(channel="devices", host="localhost", port=8086):
    #
    #initialize the PubNub handle
    #
    pub_key = os.environ['PUB_KEY']
    sub_key = os.environ['SUB_KEY']

    pubnub = Pubnub(publish_key=pub_key, subscribe_key=sub_key)
    signal.signal(signal.SIGINT, signal_handler)

    # subscribe to a channel and invoke the appropriate callback when a message arrives on that
    # channel
    #
    print("Subscribing from PubNub Channel '%s'" % (channel))
    pubnub.subscribe(channels=channel, callback=receive, error=on_error)
    pubnub.start()
def main(channel="devices", host="localhost", port=8086):
  #
  #initialize the PubNub handle
  #
  pub_key = os.environ['PUB_KEY']
  sub_key = os.environ['SUB_KEY']
  
  pubnub = Pubnub(publish_key=pub_key, subscribe_key=sub_key)
  signal.signal(signal.SIGINT, signal_handler)
  
	# subscribe to a channel and invoke the appropriate callback when a message arrives on that 
	# channel
	#
  print("Subscribing from PubNub Channel '%s'" % (channel))
  pubnub.subscribe(channels=channel, callback=receive, error=on_error)
  pubnub.start()
示例#3
0
	for msg in messages:
		pubnub.publish(channel="msg_channel", message=msg)
			
#
# callback when connected to the PubHub network
#
def on_reconnect(message):
	print ("RECONNECTED")
#
# callback whtn disconnected from the PubHub network
def on_disconnect(message):
	print ("DISCONNECTED")

if __name__ == "__main__":
	#
	# get keys from the environment variable
	#
	pub_key=os.environ['PUB_KEY']
	sub_key=os.environ['SUB_KEY']
	#
	# initate the Pubnub instance
	# subsistute your publish_key and subscribe_key here, if you want more security. But demo should wor
	pubnub = Pubnub(publish_key=pub_key, subscribe_key=sub_key)
	#
	# subscribe to a channel and invoke the appropriate callback when a message arrives on that 
	# channel
	#
	pubnub.subscribe(channels="json_channel", callback=sub_callback, error=on_error,
					connect=on_connect_json, reconnect=on_reconnect, disconnect=on_disconnect)
	pubnub.start()
示例#4
0
def error(message):
    print("ERROR : " + str(message))


def connect(message):
    print("CONNECTED " + str(message))


def reconnect(message):
    print("RECONNECTED " + str(message))


def disconnect(message):
    print("DISCONNECTED " + str(message))


print pubnub.channel_group_add_channel(channel_group='abc', channel="a")

pubnub.subscribe_group(channel_groups='abc',
                       callback=callback,
                       error=callback,
                       connect=connect,
                       reconnect=reconnect,
                       disconnect=disconnect)

#pubnub.subscribe(channels='d', callback=callback, error=callback,
#                 connect=connect, reconnect=reconnect, disconnect=disconnect)

pubnub.start()