Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On success #51

Merged
merged 5 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rustplus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@

__name__ = "rustplus"
__author__ = "olijeffers0n"
__version__ = "5.6.4"
__version__ = "5.6.5"
__support__ = "Discord: https://discord.gg/nQqJe8qvP8"
9 changes: 8 additions & 1 deletion rustplus/api/base_rust_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,17 @@ def _generate_protobuf(self) -> AppRequest:
return app_request

async def connect(
self, retries: int = float("inf"), delay: int = 20, on_failure=None
self, retries: int = float("inf"), delay: int = 20, on_failure=None, on_success=None
) -> None:
"""
Attempts to open a connection to the rust game server specified in the constructor

:param retries: The number of times to attempt reconnecting. Defaults to infinite.
:param delay: The delay (in seconds) between reconnection attempts.
:param on_failure: Optional function to be called when connecting fails.
:param on_success: Optional function to be called when connecting succeeds.


:return: None
"""
EventLoopManager.set_loop(
Expand All @@ -138,6 +144,7 @@ async def connect(
retries=retries,
delay=delay,
on_failure=on_failure,
on_success=on_success
)
await self.heartbeat.start_beat()
except ConnectionRefusedError:
Expand Down
3 changes: 2 additions & 1 deletion rustplus/api/remote/rust_remote_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ def __init__(
self.pending_entity_subscriptions = []
self.camera_manager: Union[CameraManager, None] = None

async def connect(self, retries, delay, on_failure=None) -> None:
async def connect(self, retries, delay, on_failure=None, on_success=None) -> None:
self.ws = RustWebsocket(
server_id=self.server_id,
remote=self,
use_proxy=self.use_proxy,
magic_value=self.magic_value,
use_test_server=self.use_test_server,
on_failure=on_failure,
on_success=on_success,
delay=delay,
)
await self.ws.connect(retries=retries)
Expand Down
14 changes: 14 additions & 0 deletions rustplus/api/remote/rustws.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(
magic_value,
use_test_server,
on_failure,
on_success,
delay,
):
self.connection: Union[WebSocketClientProtocol, None] = None
Expand All @@ -46,6 +47,7 @@ def __init__(
self.use_test_server = use_test_server
self.outgoing_conversation_messages = []
self.on_failure = on_failure
self.on_success = on_success
self.delay = delay

async def connect(
Expand Down Expand Up @@ -79,7 +81,19 @@ async def connect(
address += f"?v={str(self.magic_value)}"
self.connection = await connect(address, close_timeout=0, ping_interval=None)
self.connected_time = time.time()

try:
if self.on_success is not None:
if asyncio.iscoroutinefunction(self.on_success):
await self.on_success()
else:
self.on_success()

except Exception as e:
self.logger.warning(e)

break

except Exception as exception:
print_error = True

Expand Down