forked from pubnub/python
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
## www.pubnub.com - PubNub - Data Stream Network | ||
# coding=utf8 | ||
|
||
## PubNub Real-time Push APIs and Notifications Framework | ||
## Copyright (c) 2010 Stephen Blum | ||
## http://www.pubnub.com/ | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Import Libs | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
from Pubnub import PubnubTwisted as Pubnub | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Configuration | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
publish_key = 'pub-c-f6a20151-db8d-45af-ba42-def0edaa459f' | ||
subscribe_key = 'sub-c-b5ff3208-7f64-11e4-b601-02ee2ddab7fe' | ||
secret_key = 'demo' | ||
server_channel = 'echo-server' | ||
client_channel = 'echo-channel' | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Create PubNub Instance | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
pubnub = Pubnub( | ||
publish_key=publish_key, | ||
subscribe_key=subscribe_key, | ||
secret_key=secret_key, | ||
ssl_on=True | ||
) | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Error Log | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
def error_log(data): print(data) | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Access Log | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
def access_log(data): print(data) | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Respond | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
def request(): | ||
pubnub.publish( | ||
server_channel, | ||
{ 'response' : client_channel, 'body' : "Hello" }, | ||
callback=access_log, | ||
error=error_log | ||
) | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Request Received | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
def onResponse( data, channel ): | ||
print("Channel: %s | Req: %s" % (channel,data)) | ||
request() | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Ready to Receive Requests | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
def onReady(message): | ||
print("Ready to Receive Requests on '%s'" % server_channel) | ||
request() | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Network Recovered | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
def onReconnect(message): print("RECONNECTED") | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Network Failed | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
def onDisconnect(message): print("DISCONNECTED") | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Start Echo Server | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
pubnub.subscribe( | ||
client_channel, | ||
callback=onResponse, | ||
error=error_log, | ||
connect=onReady, | ||
reconnect=onReconnect, | ||
disconnect=onDisconnect | ||
) | ||
pubnub.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
## www.pubnub.com - PubNub - Data Stream Network | ||
# coding=utf8 | ||
|
||
## PubNub Real-time Push APIs and Notifications Framework | ||
## Copyright (c) 2010 Stephen Blum | ||
## http://www.pubnub.com/ | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Import Libs | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
from Pubnub import PubnubTwisted as Pubnub | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Configuration | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
publish_key = 'pub-c-f6a20151-db8d-45af-ba42-def0edaa459f' | ||
subscribe_key = 'sub-c-b5ff3208-7f64-11e4-b601-02ee2ddab7fe' | ||
secret_key = 'demo' | ||
server_channel = 'echo-server' | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Create PubNub Instance | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
pubnub = Pubnub( | ||
publish_key=publish_key, | ||
subscribe_key=subscribe_key, | ||
secret_key=secret_key, | ||
ssl_on=True | ||
) | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Error Log | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
def error_log(data): print(data) | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Access Log | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
def access_log(data): print(data) | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Respond | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
def respond( channel, body ): | ||
pubnub.publish( | ||
channel, | ||
body, | ||
callback=access_log, | ||
error=error_log | ||
) | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Request Received | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
def onRequest( request, channel ): | ||
response_channel = request['response'] | ||
response_body = request['body'] | ||
|
||
print("Channel: %s | Req: %s" % (channel,request)) | ||
|
||
respond( | ||
channel=response_channel, | ||
body=response_body | ||
) | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Ready to Receive Requests | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
def onReady(message): | ||
print("Ready to Receive Requests on '%s'" % server_channel) | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Network Recovered | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
def onReconnect(message): print("RECONNECTED") | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Network Failed | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
def onDisconnect(message): print("DISCONNECTED") | ||
|
||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
## Start Echo Server | ||
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
pubnub.subscribe( | ||
server_channel, | ||
callback=onRequest, | ||
error=error_log, | ||
connect=onReady, | ||
reconnect=onReconnect, | ||
disconnect=onDisconnect | ||
) | ||
pubnub.start() |