CloudConnection¶
Properties¶
project_id : int
¶
The ID of the project that the connection is on.
cloud_host : str
¶
The hostname of the server where the cloud variables are hosted.
Methods¶
run()
¶
Connects to the server and starts listening for variable changes.
Example:
connection = session.create_cloud_connection(193290310931, is_async=True)
@connection.on("connect")
async def on_connect():
print("Connected!")
@connection.on("set")
async def on_set(variable):
print(variable.name, variable.value)
connection.run()
await connect()
¶
Connects to the server and starts listening for variable changes. Equivalent to AsyncCloudConnection.run except it's a coroutine. Must be called with await
.
get_cloud_variable(name)
¶
Gets the value of a cloud variable with the specified name.
PARAMETERS
- name (
str
) - The name of the variable. The name does not necessarily need to include the cloud emoji ("☁️ ").
RETURNS - str
Example:
connection = session.create_cloud_connection(193290310931, is_async=True)
@connection.on("connect")
async def on_connect():
print(connection.get_cloud_variable("High score"))
# 102930921
connection.run()
await set_cloud_variable(name, value)
¶
Sets the value of a cloud variable with the specified name to the specified value. You can only do this 10 times per second. This function must be used with await
.
PARAMETERS
- name (
str
) - The name of the variable. The name does not necessarily need to include the cloud emoji ("☁️ "). - value (
str
) - The value you want to set the cloud variable to. This must be less than 256 characters long and all digits.
Example:
connection = session.create_cloud_connection(193290310931, is_async=True)
@connection.on("connect")
async def on_connect():
await connection.set_cloud_variable("High score", 102930921)
print(connection.get_cloud_variable("High score"))
# 102930921
connection.run()
await create_cloud_variable(name, initial_value=0)
¶
Creates a cloud variable with the specified name and sets it to the specified initial value. You can only do this 10 times per second. This function must be used with await
.
PARAMETERS
- name (
str
) - The name of the new variable. The name does not necessarily need to include the cloud emoji ("☁️ "). - initial_value (
int
) - The value you want to set the cloud variable to. This must be less than 256 characters long and all digits.
Example:
connection = session.create_cloud_connection(193290310931, is_async=True)
@connection.on("connect")
async def on_connect():
await connection.create_cloud_variable("High score", 10)
connection.run()
Note
This will not update live for other people using the project.
await delete_cloud_variable(name)
¶
Deletes a cloud variable with the specified name. You can only do this 10 times per second. This function must be used with await
.
PARAMETERS
- name (
str
) - The name of the variable to be deleted. The name does not necessarily need to include the cloud emoji ("☁️ ").
Example:
connection = session.create_cloud_connection(193290310931, is_async=True)
@connection.on("connect")
def on_connect():
await connection.delete_cloud_variable("High score")
connection.run()
Note
This will not update live for other people using the project.
on(key, callback=None, once=False)
¶
Adds an event for the connection listen to. This can either be used as a decorator or a function.
PARAMETERS
- key (
str
) - The key of the event to be listened to. - callback (
callable
) - The function that will run when the event occurs. - once (
bool
) - Whether the event should only be fired once.
RETURNS - None | callable
Example:
# Use as a function
async def on_set(variable):
print(variable.name, variable.value)
connection.on("set", on_set)
# Use as a decorator
@connection.on("set")
async def on_set(variable):
print(variable.name, variable.value)
off(key, callback)
¶
Removes an event that the connection was listening to.
PARAMETERS
- key (
str
) - The key of the event to be removed. - callback (
callable
) - The function that runs when the event occurs.
Example:
async def on_set(variable):
print(variable.name, variable.value)
connection.on("set", on_set)
connection.off("set", on_set)
connection.run()
once(key, callback=None)
¶
Adds an event for the connection listen to. The event will only be fired once. This can either be used as a decorator or a function.
PARAMETERS
- key (
str
) - The key of the event to be listened to. - callback (
callable
) - The function that will run when the event occurs.
RETURNS - None | callable
Example:
# Use as a function
async def on_set(variable):
print(variable.name, variable.value)
connection.once("set", on_set)
# Use as a decorator
@connection.once("set")
async def on_set(variable):
print(variable.name, variable.value)
listeners(event)
¶
Returns all the functions that are attached to the event event
.
PARAMETERS
- event (
event
) - The key of the event that you want to retrieve the listeners of.
RETURNS - list[callable]
Example:
@connection.on("set")
async def on_set(variable):
print(variable.name, variable.value)
print(connection.listeners("set"))
# <function on_set at 0x31290093>
connection.run()
Events¶
handshake
¶
Fired after the WebSocket connection handshake occurs.
connect
¶
Fired when the WebSocket connection has finished and is ready to receive data.
outgoing
¶
Fired when data is sent to the server.
PARAMETERS
- data (
str
) - The data that is being sent.
change
¶
Fired when a variable value changes, no matter who changed it.
PARAMETERS
- variable (
CloudVariable
) - The variable that has been changed, as a CloudVariable.
set
¶
Fired when a variable value changes, by anyone except yourself.
PARAMETERS
- variable (
CloudVariable
) - The variable that has been changed, as a CloudVariable.
create
¶
Fired when a cloud variable has been created.
PARAMETERS
- variable (
CloudVariable
) - The variable that has been created, as a CloudVariable.
delete
¶
Fired when a cloud variable has been deleted.
PARAMETERS
- name (
str
) - The name of the variable that has been deleted. This includes the cloud emoji at the beginning ("☁ ").