This repository has been archived by the owner on Nov 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Code reformatting for better PEP compliance * Removed redundant arguments/parameters - listener_detected and listener_id have been simplified to just listener * Implement feature parity as mentioned in #79 * Spelling fixes in comments * Various bugfixes and minor QoL enhancements. * Implement built-in/custom method safety features to mitigate security vulnerabilities.
- Loading branch information
MikeDEV
committed
Oct 29, 2022
1 parent
e8d3fc4
commit 3f710a3
Showing
10 changed files
with
1,521 additions
and
1,147 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 |
---|---|---|
@@ -1,41 +1,97 @@ | ||
from cloudlink import cloudlink | ||
|
||
class callback_examples: | ||
|
||
class example_events: | ||
def __init__(self): | ||
pass | ||
|
||
async def on_connect(self, client): | ||
#print(f"Client {client.obj_id} connected") | ||
print(f"Client {client.obj_id} connected") | ||
await client.set_username(str(client.obj_id)) | ||
await client.send_gmsg("test") | ||
|
||
async def on_close(self, client): | ||
#print(f"Client {client.obj_id} disconnected") | ||
pass | ||
|
||
print(f"Client {client.obj_id} disconnected") | ||
|
||
async def username_set(self, client): | ||
#print(f"Client {client.obj_id}'s username was set!") | ||
pass | ||
|
||
async def on_gmsg(self, client, message, listener_detected, listener_id): | ||
#print(f"Client {client.obj_id} got gmsg {message['val']}") | ||
pass | ||
print(f"Client {client.obj_id}'s username was set!") | ||
|
||
async def on_gmsg(self, client, message, listener): | ||
print(f"Client {client.obj_id} got gmsg {message['val']}") | ||
|
||
|
||
if __name__ == "__main__": | ||
# Initialize Cloudlink. You will only need to initialize one instance of the main cloudlink module. | ||
cl = cloudlink() | ||
example = callback_examples() | ||
multi_client = cl.multi_client(async_client = True, logs = True) | ||
for x in range(25): | ||
|
||
# Create examples for various ways to extend the functionality of Cloudlink Server. | ||
example = example_events() | ||
|
||
# Example - Multiple clients. | ||
multi_client = cl.multi_client(async_client=True, logs=True) | ||
|
||
# Spawns 5 clients. | ||
for x in range(5): | ||
# Create a new client object. This supports initializing many clients at once. | ||
client = multi_client.spawn(x, "ws://127.0.0.1:3000/") | ||
|
||
# Binding events - This example binds functions to certain events | ||
client.bind_event(client.events.on_connect, example.on_connect) # When a client connects, all functions bound to this event will fire. | ||
client.bind_event(client.events.on_close, example.on_close) # When a client disconnects, all functions bound to this event will fire. | ||
client.bind_event(client.events.on_username_set, example.username_set) # When a client disconnects, all functions bound to this event will fire. | ||
|
||
# When a client connects, all functions bound to this event will fire. | ||
client.bind_event( | ||
client.events.on_connect, | ||
example.on_connect | ||
) | ||
|
||
# When a client disconnects, all functions bound to this event will fire. | ||
client.bind_event( | ||
client.events.on_close, | ||
example.on_close | ||
) | ||
|
||
# When a client disconnects, all functions bound to this event will fire. | ||
client.bind_event( | ||
client.events.on_username_set, | ||
example.username_set | ||
) | ||
|
||
# Binding callbacks for commands - This example binds an event when a gmsg packet is handled. | ||
client.bind_callback_method(client.cl_methods.gmsg, example.on_gmsg) | ||
|
||
print("Waking up now") | ||
multi_client.run() | ||
input("All clients are ready. Press enter to shutdown.") | ||
multi_client.stop() | ||
input("All clients have shut down. Press enter to exit.") | ||
input("All clients have shut down. Press enter to exit.") | ||
|
||
# Example - Singular clients. | ||
|
||
# Create a new client object. | ||
client = cl.client(async_client=True, logs=True) | ||
client.obj_id = "Test" | ||
|
||
# Binding events - This example binds functions to certain events | ||
|
||
# When a client connects, all functions bound to this event will fire. | ||
client.bind_event( | ||
client.events.on_connect, | ||
example.on_connect | ||
) | ||
|
||
# When a client disconnects, all functions bound to this event will fire. | ||
client.bind_event( | ||
client.events.on_close, | ||
example.on_close | ||
) | ||
|
||
# When a client disconnects, all functions bound to this event will fire. | ||
client.bind_event( | ||
client.events.on_username_set, | ||
example.username_set | ||
) | ||
|
||
# Binding callbacks for commands - This example binds an event when a gmsg packet is handled. | ||
client.bind_callback_method(client.cl_methods.gmsg, example.on_gmsg) | ||
|
||
# Run the client. | ||
client.run("ws://127.0.0.1:3000/") |
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 |
---|---|---|
@@ -1,41 +1,97 @@ | ||
from cloudlink import cloudlink | ||
|
||
class callback_examples: | ||
|
||
class example_events: | ||
def __init__(self): | ||
pass | ||
|
||
def on_connect(self, client): | ||
#print(f"Client {client.obj_id} connected") | ||
print(f"Client {client.obj_id} connected") | ||
client.set_username(str(client.obj_id)) | ||
client.send_gmsg("test") | ||
|
||
def on_close(self, client): | ||
#print(f"Client {client.obj_id} disconnected") | ||
pass | ||
|
||
print(f"Client {client.obj_id} disconnected") | ||
|
||
def username_set(self, client): | ||
#print(f"Client {client.obj_id}'s username was set!") | ||
pass | ||
|
||
def on_gmsg(self, client, message, listener_detected, listener_id): | ||
#print(f"Client {client.obj_id} got gmsg {message['val']}") | ||
pass | ||
print(f"Client {client.obj_id}'s username was set!") | ||
|
||
def on_gmsg(self, client, message, listener): | ||
print(f"Client {client.obj_id} got gmsg {message['val']}") | ||
|
||
|
||
if __name__ == "__main__": | ||
# Initialize Cloudlink. You will only need to initialize one instance of the main cloudlink module. | ||
cl = cloudlink() | ||
example = callback_examples() | ||
multi_client = cl.multi_client(async_client = False, logs = True) | ||
for x in range(25): | ||
|
||
# Create examples for various ways to extend the functionality of Cloudlink Server. | ||
example = example_events() | ||
|
||
# Example - Multiple clients. | ||
multi_client = cl.multi_client(async_client=False, logs=True) | ||
|
||
# Spawns 5 clients. | ||
for x in range(5): | ||
# Create a new client object. This supports initializing many clients at once. | ||
client = multi_client.spawn(x, "ws://127.0.0.1:3000/") | ||
|
||
# Binding events - This example binds functions to certain events | ||
client.bind_event(client.events.on_connect, example.on_connect) # When a client connects, all functions bound to this event will fire. | ||
client.bind_event(client.events.on_close, example.on_close) # When a client disconnects, all functions bound to this event will fire. | ||
client.bind_event(client.events.on_username_set, example.username_set) # When a client disconnects, all functions bound to this event will fire. | ||
|
||
# When a client connects, all functions bound to this event will fire. | ||
client.bind_event( | ||
client.events.on_connect, | ||
example.on_connect | ||
) | ||
|
||
# When a client disconnects, all functions bound to this event will fire. | ||
client.bind_event( | ||
client.events.on_close, | ||
example.on_close | ||
) | ||
|
||
# When a client disconnects, all functions bound to this event will fire. | ||
client.bind_event( | ||
client.events.on_username_set, | ||
example.username_set | ||
) | ||
|
||
# Binding callbacks for commands - This example binds an event when a gmsg packet is handled. | ||
client.bind_callback_method(client.cl_methods.gmsg, example.on_gmsg) | ||
|
||
print("Waking up now") | ||
multi_client.run() | ||
input("All clients are ready. Press enter to shutdown.") | ||
multi_client.stop() | ||
input("All clients have shut down. Press enter to exit.") | ||
input("All clients have shut down. Press enter to exit.") | ||
|
||
# Example - Singular clients. | ||
client = cl.client(async_client=False, logs=True) | ||
|
||
# Object IDs - Sets a friendly name to a specific client object. | ||
client.obj_id = "Test" | ||
|
||
# Binding events - This example binds functions to certain events | ||
|
||
# When a client connects, all functions bound to this event will fire. | ||
client.bind_event( | ||
client.events.on_connect, | ||
example.on_connect | ||
) | ||
|
||
# When a client disconnects, all functions bound to this event will fire. | ||
client.bind_event( | ||
client.events.on_close, | ||
example.on_close | ||
) | ||
|
||
# When a client disconnects, all functions bound to this event will fire. | ||
client.bind_event( | ||
client.events.on_username_set, | ||
example.username_set | ||
) | ||
|
||
# Binding callbacks for commands - This example binds an event when a gmsg packet is handled. | ||
client.bind_callback_method(client.cl_methods.gmsg, example.on_gmsg) | ||
|
||
# Run the client. | ||
client.run("ws://127.0.0.1:3000/") |
Oops, something went wrong.