本文整理汇总了Python中pubnub.Pubnub.subscribe方法的典型用法代码示例。如果您正苦于以下问题:Python Pubnub.subscribe方法的具体用法?Python Pubnub.subscribe怎么用?Python Pubnub.subscribe使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pubnub.Pubnub
的用法示例。
在下文中一共展示了Pubnub.subscribe方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Logs
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import subscribe [as 别名]
class Logs(object):
def __init__(self):
self.config = Config()
def init_pubnub(func):
def wrapper(self, *args, **kwargs):
if not hasattr(self, 'pubnub'):
pubnub_key = self.config.get_pubnub_keys()
self.pubnub = Pubnub(publish_key=pubnub_key['publish_key'], subscribe_key=pubnub_key['subscribe_key'])
return func(self, *args, **kwargs)
return wrapper
@init_pubnub
def subscribe(self, uuid, callback, error):
channel = self.get_channel(uuid)
self.pubnub.subscribe(channels=channel, callback=callback, error=error)
@init_pubnub
def history(self, uuid, callback, error):
channel = self.get_channel(uuid)
self.pubnub.history(channel=channel, callback=callback, error=error)
def unsubscribe(self, uuid):
if hasattr(self, 'pubnub'):
channel = self.get_channel(uuid)
self.pubnub.unsubscribe(channel=channel)
@staticmethod
def get_channel(uuid):
return 'device-{uuid}-logs'.format(uuid=uuid)
示例2: pub_Init
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import subscribe [as 别名]
def pub_Init():
global pubnub
try:
pubnub = Pubnub(publish_key=pub_key,subscribe_key=sub_key)
pubnub.subscribe(channels='trail2pub_channel', callback=callback,error=error,
connect=connect, reconnect=reconnect, disconnect=disconnect)
except Exception as pubException:
print colored("The pubException is %s %s"%(pubException,type(pubException)),'red','on_white',attrs=['bold'])
示例3: manageGPSLocation
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import subscribe [as 别名]
class manageGPSLocation(Thread) :
test = os.environ
pubnub_publish_key = os.environ['PUBNUB_PUBLISH_KEY']
pubnub_subscribe_key = os.environ['PUBNUB_SUBSCRIBE_KEY']
mongodb_connection = None
collection = None
def __init__(self):
Thread.__init__(self)
mongodb_connection = ConnectionToDatabase()
manageGPSLocation.collection = mongodb_connection.getCollection("steeds")
self.pubnub_settings = Pubnub(publish_key=manageGPSLocation.pubnub_publish_key,subscribe_key=manageGPSLocation.pubnub_subscribe_key)
# Rename to location channel
self.pubnub_channel = "channel_test"
self.genericDAO = GenericDAO()
def subscriber_callback(self, message, channel):
print(message)
criteriaDict = {}
criteriaDict["_id"]=message["_id"]
updateDict = {}
subUpdateDict = {}
subUpdateDict["type"]="Point"
subUpdateDict["coordinates"] = [message["lng"],message["lat"]]
updateDict["location"]=subUpdateDict
self.genericDAO.updateObjects(manageGPSLocation.collection, criteriaDict, updateDict)
def subscriber_error(self, message):
print("ERROR : "+message)
def connect(self, message):
print("CONNECTED TO LOCATION CHANNEL")
def reconnect(self, message):
print("RECONNECTED TO LOCATION CHANNEL")
def disconnect(self, message):
print("DISCONNECTED FROM LOCATION CHANNEL")
def subscribe(self):
# souscrire au channel
self.pubnub_settings.subscribe(channels=self.pubnub_channel, callback=self.subscriber_callback, error=self.subscriber_error
,connect=self.connect, reconnect=self.reconnect, disconnect=self.disconnect)
def publish(self, message):
self.pubnub_settings.publish(channel=self.pubnub_channel, message=message)
def unsubscribe(self):
# se desinscire du channel
self.pubnub_settings.unsubscribe(self.pubnub_channel)
示例4: run
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import subscribe [as 别名]
def run(callback):
pubnub = Pubnub(
'demo',
'sub-c-78806dd4-42a6-11e4-aed8-02ee2ddab7fe'
)
pubnub.subscribe(
channels='pubnub-twitter',
callback= callback
)
示例5: __init__
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import subscribe [as 别名]
class Client:
command_key = "command"
def __init__(self, channel):
self.channel = channel
# Publish key is the one that usually starts with the "pub-c-" prefix
publish_key = "pub-c-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
# Subscribe key is the one that usually starts with the "sub-c" prefix
# Do not forget to replace the string with your subscribe key
subscribe_key = "sub-c-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
self.pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key)
self.pubnub.subscribe(channels=self.channel,
callback=self.callback,
error=self.callback,
connect=self.connect,
reconnect=self.reconnect,
disconnect=self.disconnect)
def callback_command_message(self, message):
print("I've received the following response from PubNub cloud: {0}".format(message))
def error_command_message(self, message):
print("There was an error when working with the PubNub cloud: {0}".format(message))
def publish_command(self, command_name, key, value):
command_message = {
self.__class__.command_key: command_name,
key: value}
self.pubnub.publish(
channel=self.channel,
message=command_message,
callback=self.callback_command_message,
error=self.error_command_message)
def callback(self, message, channel):
if channel == self.channel:
print("I've received the following message: {0}".format(message))
def error(self, message):
print("Error: " + str(message))
def connect(self, message):
print("Connected to the {0} channel".
format(self.channel))
print(self.pubnub.publish(
channel=self.channel,
message="Listening to messages in the PubNub Python Client"))
def reconnect(self, message):
print("Reconnected to the {0} channel".
format(self.channel))
def disconnect(self, message):
print("Disconnected from the {0} channel".
format(self.channel))
示例6: manageClients
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import subscribe [as 别名]
class manageClients(Thread) :
test = os.environ
pubnub_publish_key = os.environ['PUBNUB_PUBLISH_KEY']
pubnub_subscribe_key = os.environ['PUBNUB_SUBSCRIBE_KEY']
mongodb_connection = None
collection = None
def __init__(self):
Thread.__init__(self)
mongodb_connection = ConnectionToDatabase()
manageClients.deliveries_collection = mongodb_connection.getCollection("temp_deliveries")
self.pubnub_settings = Pubnub(publish_key=manageClients.pubnub_publish_key,subscribe_key=manageClients.pubnub_subscribe_key)
# Rename to location channel
self.pubnub_channel = "clients_channel"
self.genericDAO = GenericDAO()
self.manageSteeds = manageSteeds()
def subscriber_callback(self, message, channel):
print(message)
# Inserer la demande de livraison faite par le client
if(message["msg_code"] == "XX") :
# inserer la demande de livraison dans une collection temporaire
id_delivery = self.genericDAO.insertObject(manageClients.deliveries_collection, message)
def subscriber_error(self, message):
print("ERROR : "+message)
def connect(self, message):
print("CONNECTED TO LOCATION CHANNEL")
def reconnect(self, message):
print("RECONNECTED TO LOCATION CHANNEL")
def disconnect(self, message):
print("DISCONNECTED FROM LOCATION CHANNEL")
def subscribe(self):
# souscrire au channel
self.pubnub_settings.subscribe(channels=self.pubnub_channel, callback=self.subscriber_callback, error=self.subscriber_error
,connect=self.connect, reconnect=self.reconnect, disconnect=self.disconnect)
def publish(self, message):
self.pubnub_settings.publish(channel=self.pubnub_channel, message=message)
def unsubscribe(self):
# se desinscire du channel
self.pubnub_settings.unsubscribe(self.pubnub_channel)
示例7: __init__
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import subscribe [as 别名]
def __init__(self, wink):
"""Initialize the Wink device."""
from pubnub import Pubnub
self.wink = wink
self._battery = self.wink.battery_level
if self.wink.pubnub_channel in CHANNELS:
pubnub = Pubnub("N/A", self.wink.pubnub_key, ssl_on=True)
pubnub.set_heartbeat(120)
pubnub.subscribe(self.wink.pubnub_channel, self._pubnub_update, error=self._pubnub_error)
else:
CHANNELS.append(self.wink.pubnub_channel)
SUBSCRIPTION_HANDLER.subscribe(self.wink.pubnub_channel, self._pubnub_update, error=self._pubnub_error)
示例8: PiBusHub
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import subscribe [as 别名]
class PiBusHub(object):
def __init__(self):
self.logger = logging.getLogger(__name__)
if not hasattr(cfg, 'PUBNUB_CHANNEL_KEY'):
self.logger.debug("PubNub[ChannelKey] not defined - PubNub support disabled")
if not hasattr(cfg, 'PUBNUB_PUBLISH_KEY'):
self.logger.debug("PubNub[PublishKey] not defined - PubNub support disabled")
return
if cfg.PUBNUB_PUBLISH_KEY == '':
self.logger.debug("PubNub[PublishKey] not configured - PubNub support disabled")
return
if not hasattr(cfg, 'PUBNUB_SUBSCRIBE_KEY'):
self.logger.debug("PubNub[SubscribeKey] not defined - PubNub support disabled")
return
self.logger.debug("PubNub registering: " +cfg.PUBNUB_PUBLISH_KEY + " : " + cfg.PUBNUB_SUBSCRIBE_KEY)
self.pubnub = Pubnub(publish_key=cfg.PUBNUB_PUBLISH_KEY, subscribe_key=cfg.PUBNUB_SUBSCRIBE_KEY)
if cfg.PUBNUB_SUBSCRIBE_KEY != '':
self.logger.info("PubNub subscribing: " + cfg.PUBNUB_CHANNEL_KEY)
self.pubnub.subscribe(channels=cfg.PUBNUB_CHANNEL_KEY, callback=self.callback, error=self.error, connect=self.connect, reconnect=self.reconnect, disconnect=self.disconnect)
def callback(self, message, channel):
print("CALLBACK: " + message +":" + channel)
def error(message):
print("ERROR : " + str(message))
def connect(self, channel):
print("CONNECTED: " +channel)
self.pubnub.publish(channel='Mongo', message='Hello from the PubNub Python SDK')
def reconnect(message):
print("RECONNECTED:" + message)
def disconnect(message):
print("DISCONNECTED:" + message)
def publish(self, message):
print("PUBLISH:" + message)
self.pubnub.publish(channel=cfg.PUBNUB_CHANNEL_KEY, message=message)
示例9: init
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import subscribe [as 别名]
def init():
#Pubnub Initialization
global pubnub,client
pubnub = Pubnub(publish_key=PUB_KEY,subscribe_key=SUB_KEY)
pubnub.subscribe(channels='garbageApp-req', callback=appcallback, error=appcallback, reconnect=reconnect, disconnect=disconnect)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(HOST_IP, 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
示例10: main
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import subscribe [as 别名]
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()
示例11: __init__
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import subscribe [as 别名]
class WorkspaceSensor:
def __init__(self,setting_file_path):
with open(setting_file_path) as f:
self._setting = json.load(f,"utf-8")
self._circuit = circuit.parse(self._setting["GPIO"])
self._current_status = self._circuit.get_status()
self._on_resistance = Resistance(self._setting["on_resistance"],1,self._update_status)
self._off_resistance = Resistance(self._setting["off_resistance"],0,self._update_status)
self._pubnub = Pubnub(publish_key=self._setting["publish_key"], subscribe_key=self._setting["subscribe_key"])
self._pubnub.subscribe(channels='plzcast_' + self._setting["group"], callback=self._callback_plzcast,connect=self._callback_connect, reconnect=self._callback_connect)
def run(self):
while True:
sensor_status = self._circuit.get_status()
if(sensor_status == 1):
self._on_resistance.load()
self._off_resistance.clear()
else:
self._off_resistance.load()
self._on_resistance.clear()
time.sleep(self._setting["duration"])
def _send(self):
message=json.dumps({
"floor":self._setting["floor"],
"id":self._setting["id"],
"name":self._setting["name"],
"status":self._current_status
})
self._pubnub.publish(channel='wkstatus_' + self._setting["group"],message=message)
def _callback_plzcast(self,message,channel):
self._send()
def _callback_connect(self,message):
self._send()
def _update_status(self,new_status):
if(new_status != self._current_status):
self._current_status = new_status
self._send()
示例12: connectPubnub
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import subscribe [as 别名]
def connectPubnub():
global pn_connected, pn
try:
# Setup pubnub channel
pn = Pubnub(publish_key=pn_publish_key, subscribe_key=pn_subscribe_key)
# Setup pubunb listener
pn.subscribe(channels=pn_channel, callback=onPubnubMessage)
# Flag initialized
print "Connected to Pubnub channel:", pn_channel
pn_connected = True
except Exception, e:
print "Unable to connect to Pubnub, retrying..."
time.sleep(5)
connectPubnub()
示例13: ServoCommandListener
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import subscribe [as 别名]
class ServoCommandListener(object):
def __init__(self, servo_controller):
self.servo_controller = servo_controller
self.pubnub = Pubnub(
publish_key=PUBNUB_PUBLISH_KEY,
subscribe_key=PUBNUB_SUBSCRIBE_KEY,
cipher_key='',
ssl_on=False
)
self.pubnub.subscribe(LISTEN_CHANNEL, self.callback)
def callback(self, message, channel):
# not the best code for sure...
self.servo_controller.action_from_strings(
message['command'],
message['value'],
)
示例14: iotbridge
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import subscribe [as 别名]
class iotbridge(object):
def __init__(self, publish_key,
subscribe_key,uuid):
self.publish_key = publish_key
self.subscribe_key = subscribe_key
self.uuid = uuid
self.pubnub = Pubnub( 'demo', 'demo', None, False)
self.pubnub.uuid = self.uuid
def send(self, channel, message):
# Sending message on the channel
self.pubnub.publish(channel, message)
def connect(self, channel, callback):
# Listening for messages on the channel
self.pubnub.subscribe(channel, callback=callback)
示例15: callback
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import subscribe [as 别名]
class Net:
def callback(self, message):
print(message)
def __init__(self, channel = "Channel-ux8kwdv1u"):
self.channel = channel
self.pubnub = Pubnub(publish_key=PUB_KEY,
subscribe_key=SUB_KEY)
def subscribe(self, callback):
self.pubnub.subscribe(channels=self.channel,
callback=callback, error=self.callback)
def unsubscribe(self):
self.pubnub.unsubscribe(channel=self.channel)
def publish(self, message):
self.pubnub.publish(channel=self.channel, message=message,
callback=self.callback, error=self.callback)