This repository has been archived by the owner on Jan 13, 2024. It is now read-only.
forked from MikeDev101/cloudlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_example.py
executable file
·57 lines (42 loc) · 1.82 KB
/
server_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from cloudlink import CloudLink
"""
CloudLink Server Example
This demonstrates the new features of CloudLink server as of 0.1.7.2.
For more information, please visit https://hackmd.io/G9q1kPqvQT6NrPobjjxSgg
"""
def on_packet(message):
# We can get the IP of the client
if type(message["id"]) == dict:
print("Username is not set for this ID, ID is {0}".format(message["id"]["id"]))
print("IP of unnamed user is {0}".format(cl.getIPofObject(message["id"])))
elif type(message["id"]) == str:
print("Username set as {0}".format(message["id"]))
print("IP of user is {0}".format(cl.getIPofUsername(message["id"])))
# Check for custom commands, of which we can write custom handlers for
if "cmd" in message:
print("Detected custom cmd")
print("cmd is {0}".format(message["cmd"]))
print("val is", message["val"])
# Send back a status code to the user
cl.sendPacket({"cmd": "statuscode", "val": cl.codes["OK"], "id": message["id"]})
#cl.sendPacket({"cmd": "direct", "val": message["val"], "id": message["id"]})
def on_connect(client):
print("New client connected:", client["id"])
def on_error(error):
print("Got an error: {0}".format(error))
def on_close(client):
print("Client disconnected:", client["id"])
if __name__ == "__main__":
cl = CloudLink(debug=False)
# Instanciate CloudLink
cl.callback("on_packet", on_packet)
cl.callback("on_error", on_error)
cl.callback("on_connect", on_connect)
cl.callback("on_close", on_close)
# Specify callbacks to functions above
#cl.trustedAccess(True, ["test"])
# Enable trusted access
cl.setMOTD("CloudLink test", enable=True)
# Turn on Message-Of-The-Day
cl.server()
# Run the server