def _server_presence_callback(message, channel): print message if 'action' in message: if message['action'] == 'join' and message['uuid'] == client_uuid: print "Server can see that client with UUID " + message['uuid'] + " has a state of " + json.dumps(message['data']) def _client_presence_callback(message, channel): print message if 'action' in message: if message['action'] == 'join' and message['uuid'] == client_uuid: print "Client can see that client with UUID " + message['uuid'] + " has a state of " + json.dumps(message['data']) # server subscribes to public channel server.subscribe(channels=channel_public, callback=_server_message_callback, error=_server_error_callback) # server subscribes to presence events on public channel # presence() is a convienence method that subscribes to channel-pnpres with special logic for handling # presence-event formatted messages ## uncomment out to see server able to read on presence channel server.presence(channel=channel_public, callback=_server_presence_callback, error=_server_error_callback) # now if the client tried to subscribe on the presence channel, and therefore, get state info # he is explicitly denied! ## uncomment out to see client not able to read on presence channel client.presence(channel=channel_public, callback=_client_presence_callback, error=_client_error_callback) # client subscribes to public channel client.subscribe(channels=channel_public, state={ "myKey" : get_unique("foo")}, callback=_client_message_callback, error=_client_error_callback)
## PubNub Real-time Push APIs and Notifications Framework ## Copyright (c) 2010 Stephen Blum ## http://www.pubnub.com/ import sys from pubnub import Pubnub publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo' subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo' secret_key = len(sys.argv) > 3 and sys.argv[3] or 'demo' cipher_key = len(sys.argv) > 4 and sys.argv[4] or '' ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False import time ## ----------------------------------------------------------------------- ## Initiate Pubnub State ## ----------------------------------------------------------------------- pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key, secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on, daemon=False) channel = 'b' # Asynchronous usage def callback(message, channel): print(message) pubnub.presence(channel, callback=callback)
print("Client can see that client with UUID " + message['uuid'] + " has a state of " + json.dumps(message['data'])) # server subscribes to public channel server.subscribe(channels=channel_public, callback=_server_message_callback, error=_server_error_callback) # server subscribes to presence events on public channel # presence() is a convienence method that subscribes to channel-pnpres with special logic for handling # presence-event formatted messages # uncomment out to see server able to read on presence channel server.presence(channel=channel_public, callback=_server_presence_callback, error=_server_error_callback) # now if the client tried to subscribe on the presence channel, and therefore, get state info # he is explicitly denied! # uncomment out to see client not able to read on presence channel client.presence(channel=channel_public, callback=_client_presence_callback, error=_client_error_callback) # client subscribes to public channel client.subscribe(channels=channel_public, state={"myKey": get_unique("foo")}, callback=_client_message_callback, error=_client_error_callback)
# http://www.pubnub.com/ import sys from pubnub import Pubnub publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo' subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo' secret_key = len(sys.argv) > 3 and sys.argv[3] or 'demo' cipher_key = len(sys.argv) > 4 and sys.argv[4] or '' ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False # ----------------------------------------------------------------------- # Initiate Pubnub State # ----------------------------------------------------------------------- pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key, secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on, daemon=False) channel = 'b' # Asynchronous usage def callback(message, channel): print(message) pubnub.presence(channel, callback=callback)
# Callback function that is executed when presence changes on PubNub network def presence_callback(message, channel): print(message) # Turn off all lights (52 - 67 corresponds to lights on second page of MF3D) for note in range(52, 67): output_MF3D.send(mido.Message('note_off', note=note, velocity=127)) # Turn on number of lights equal to occupancy for i in range(message['occupancy']): output_MF3D.send(mido.Message('note_on', note=52 + i, velocity=127)) print("Entering main loop. Press Control-C to exit.") try: # Monitor presence for changes in occupancy pubnub.presence(channel=channel, callback=presence_callback) # Poll the MF3D infinitely for MIDI messages while True: msg = midiin.get_message() # Only consider messages with actual note information if msg: message, deltatime = msg print("%r" % message) # Timestamp to calculate roundtrip latency if message[0] == 144: stamp = datetime.utcnow() # Data to be transmitted. Parse "message" list into constituent parts