-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expoential as a default reconnection policy (#196)
* Expoential as a default reconnection policy * Add respect of user defined retry limit * That one test which still needs no reconnect policy * Add custom delay for linear policy * clean up old variables
- Loading branch information
Showing
15 changed files
with
599 additions
and
50 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,50 @@ | ||
import logging | ||
import sys | ||
import time | ||
|
||
from pubnub.pnconfiguration import PNConfiguration | ||
from pubnub.pubnub import PubNub, SubscribeListener | ||
from pubnub.enums import PNReconnectionPolicy, PNStatusCategory | ||
|
||
|
||
class TestListener(SubscribeListener): | ||
status_result = None | ||
disconnected = False | ||
|
||
def status(self, pubnub, status): | ||
if status.category == PNStatusCategory.PNDisconnectedCategory: | ||
print('Could not connect. Exiting...') | ||
self.disconnected = True | ||
|
||
def message(self, pubnub, message): | ||
print(f'Message:\n{message.__dict__}') | ||
|
||
def presence(self, pubnub, presence): | ||
print(f'Presence:\n{presence.__dict__}') | ||
|
||
|
||
logger = logging.getLogger("pubnub") | ||
logger.setLevel(logging.DEBUG) | ||
handler = logging.StreamHandler(sys.stdout) | ||
handler.setLevel(logging.DEBUG) | ||
logger.addHandler(handler) | ||
|
||
|
||
config = PNConfiguration() | ||
config.subscribe_key = "demo" | ||
config.publish_key = "demo" | ||
config.user_id = 'example' | ||
config.enable_subscribe = True | ||
config.reconnect_policy = PNReconnectionPolicy.EXPONENTIAL | ||
config.origin = '127.0.0.1' | ||
config.ssl = False | ||
|
||
listener = TestListener() | ||
|
||
pubnub = PubNub(config) | ||
pubnub.add_listener(listener) | ||
sub = pubnub.subscribe().channels(['example']).execute() | ||
|
||
while not listener.disconnected: | ||
time.sleep(0.5) | ||
print('Disconnected. Bye.') |
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,59 @@ | ||
import asyncio | ||
import logging | ||
import sys | ||
|
||
from pubnub.callbacks import SubscribeCallback | ||
from pubnub.models.consumer.common import PNStatus | ||
from pubnub.pubnub_asyncio import PubNubAsyncio | ||
from pubnub.pnconfiguration import PNConfiguration | ||
from pubnub.enums import PNReconnectionPolicy, PNStatusCategory | ||
|
||
config = PNConfiguration() | ||
config.subscribe_key = "demo" | ||
config.publish_key = "demo" | ||
config.enable_subscribe = True | ||
config.uuid = "test-uuid" | ||
config.origin = "127.0.0.1" | ||
config.ssl = False | ||
config.reconnect_policy = PNReconnectionPolicy.NONE | ||
|
||
pubnub = PubNubAsyncio(config) | ||
|
||
logger = logging.getLogger("pubnub") | ||
logger.setLevel(logging.WARNING) | ||
handler = logging.StreamHandler(sys.stdout) | ||
handler.setLevel(logging.WARNING) | ||
logger.addHandler(handler) | ||
|
||
|
||
class SampleCallback(SubscribeCallback): | ||
message_result = None | ||
status_result = None | ||
presence_result = None | ||
|
||
def status(self, pubnub, status): | ||
self.status_result = status | ||
|
||
def message(self, pubnub, message): | ||
self.message_result = message | ||
|
||
def presence(self, pubnub, presence): | ||
self.presence_result = presence | ||
|
||
|
||
async def main(): | ||
listener = SampleCallback() | ||
pubnub.add_listener(listener) | ||
pubnub.subscribe().channels("my_channel").execute() | ||
while True: | ||
if isinstance(listener.status_result, PNStatus) \ | ||
and listener.status_result.category == PNStatusCategory.PNDisconnectedCategory: | ||
print('Could not connect. Exiting...') | ||
break | ||
await asyncio.sleep(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(main()) | ||
loop.close() |
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
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
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
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
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
Oops, something went wrong.